🔷 C# 치트시트

Microsoft의 객체지향 프로그래밍 언어

기본 문법

변수 선언
int number = 42;
double price = 19.99;
string name = "C#";
bool isActive = true;
var autoType = "자동 타입 추론";
조건문
if (condition)
{
    statement;
}
else if (otherCondition)
{
    otherStatement;
}
else
{
    defaultStatement;
}
반복문
for (int i = 0; i < 10; i++)
{
    Console.WriteLine(i);
}

foreach (var item in collection)
{
    Console.WriteLine(item);
}
메서드 정의
public static int Add(int a, int b)
{
    return a + b;
}

public void PrintMessage(string message)
{
    Console.WriteLine(message);
}

클래스와 객체

클래스 정의
public class Person
{
    public string Name { get; set; }
    public int Age { get; private set; }
    
    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }
}
상속
public class Student : Person
{
    public string School { get; set; }
    
    public Student(string name, int age, string school) : base(name, age)
    {
        School = school;
    }
}
인터페이스
public interface IDrawable
{
    void Draw();
}

public class Circle : IDrawable
{
    public void Draw()
    {
        Console.WriteLine("원을 그립니다");
    }
}
추상 클래스
public abstract class Animal
{
    public abstract void MakeSound();
    
    public virtual void Sleep()
    {
        Console.WriteLine("잠을 잡니다");
    }
}

LINQ와 컬렉션

LINQ 쿼리
var numbers = new List<int> { 1, 2, 3, 4, 5 };
var evenNumbers = numbers.Where(n => n % 2 == 0).ToList();
var sum = numbers.Sum();
var max = numbers.Max();
딕셔너리
var scores = new Dictionary<string, int>
{
    ["Alice"] = 95,
    ["Bob"] = 87,
    ["Charlie"] = 92
};

int aliceScore = scores["Alice"];
scores["David"] = 88;
리스트
var names = new List<string> { "Alice", "Bob" };
names.Add("Charlie");
names.Remove("Bob");
bool containsAlice = names.Contains("Alice");
배열
int[] numbers = { 1, 2, 3, 4, 5 };
string[] names = new string[3];
names[0] = "Alice";
names[1] = "Bob";
names[2] = "Charlie";
더 많은 C# 학습 자료

체계적인 학습을 위해 다음 자료들도 확인해보세요