📍C#

1. C# 소개

C#("C 샤프"로 발음)은 Microsoft에서 개발한 프로그래밍 언어입니다. .NET 프레임워크의 일부인 다재다능하고 현대적인 프로그래밍 언어입니다. C#은 웹, 데스크톱, 모바일 및 게임 응용 프로그램을 비롯한 다양한 소프트웨어 응용 프로그램을 개발하는 데 널리 사용됩니다.

2. C# 언어 기본개념

코멘트:

  • 한 줄 주석: 줄 끝까지 확장되는 주석을 추가하려면 이중 슬래시(//)를 사용합니다.

// This is a single-line comment.
  • 여러 줄 주석: 여러 줄에 걸쳐 주석을 /**/로 묶습니다.

/* This is a
   multi-line comment. */

변수 및 데이터 유형:

  • 변수: 'type variableName = value;' 구문을 사용하여 변수를 선언하고 초기화합니다.

int age = 25;                 // Integer
double salary = 2500.50;      // Double
string name = "EZEN";         // String
bool isEmployed = true;       // Boolean
char grade = 'A';             // Character

제어 구조:

  • if` 문: 지정된 조건에 따라 코드의 조건부 실행을 허용합니다.

if (age >= 18)
{
    Console.WriteLine("You are an adult.");
}
  • 'else if' 문: 이전 조건이 거짓인지 확인하기 위해 추가 조건에 사용됩니다.

if (score >= 90)
{
    Console.WriteLine("Excellent!");
}
else if (score >= 80)
{
    Console.WriteLine("Good job!");
}
  • 'else' 문: 이전 조건 중 어느 것도 참이 아닐 때 실행됩니다.

if (isRaining)
{
    Console.WriteLine("Take an umbrella.");
}
else
{
    Console.WriteLine("Enjoy the weather!");
}
  • for 루프: 특정 횟수만큼 코드 블록을 실행합니다.

for (int i = 0; i < 5; i++)
{
    Console.WriteLine(i);
}
  • while 루프: 조건이 참인 동안 코드 블록을 반복적으로 실행합니다.

int count = 0;
while (count < 10)
{
    Console.WriteLine(count);
    count++;
}

함수와 메서드

  • 함수: 특정 작업을 수행하고 선택적으로 값을 반환하는 코드 블록입니다.

int Add(int a, int b)
{
    return a + b;
}
  • 메소드: 함수와 유사하지만 클래스 또는 객체에 속합니다.

public void Greet(string name)
{
    Console.WriteLine("Hello, " + name + "!");
}

클래스 및 개체

  • 클래스: 객체 생성을 위한 청사진 또는 템플릿. 데이터와 동작을 캡슐화합니다.

class Circle
{
    double radius;
    
    public double CalculateArea()
    {
        return Math.PI * radius * radius;
    }
}
  • 개체: new 키워드를 사용하여 만들 수 있는 클래스의 인스턴스입니다.

Circle myCircle = new Circle();
myCircle.radius = 5;
double area = myCircle.CalculateArea();

입력과 출력:

  • Console.WriteLine(): 콘솔에 한 줄의 텍스트를 출력합니다.

Console.WriteLine("Hello, World!");
  • Console.ReadLine(): 콘솔에서 입력 라인을 읽고 문자열로 반환합니다.

Console.WriteLine("Enter your name:");
string name = Console.ReadLine();

예외 처리:

  • try-catch 블록: 프로그램 실행 중 발생할 수 있는 예외(오류)를 처리하는 데 사용됩니다.

try
{
    int result = Divide(10, 0);
    Console.WriteLine(result);
}
catch (DivideByZeroException ex)
{
    Console.WriteLine("Error: " + ex.Message);
}
#

Last updated