본문 바로가기

워크/C# 기본 문법

(18)
C#의 기본 문법의 예시 코드 아래는 C#의 기본 문법에서 예시로 설명한 예시 코드만 나열하였습니다. using System; using System.Collections.Generic; using System.Linq; namespace CSharpExample { class Program { static void Main(string[] args) { // 변수와 자료형 int age = 30; string name = "John"; // 상수 const double PI = 3.14159; // 배열 int[] numbers = { 1, 2, 3, 4, 5 }; // 조건문 if (age >= 20) { Console.WriteLine("Adult"); } else { Console.WriteLine("Not Adult"); }..
LINQ C#에서 LINQ를 사용하는 예시는 다음과 같습니다: using System; using System.Collections.Generic; using System.Linq; namespace LINQExample { class Program { static void Main(string[] args) { List numbers = new List { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // LINQ 쿼리식 사용 var evenNumbers = from number in numbers where number % 2 == 0 select number; // 결과 출력 foreach (var number in evenNumbers) { Console.WriteLine(number); }..
람다 식 C#에서 람다 식을 사용하는 예시는 다음과 같습니다: using System; using System.Collections.Generic; using System.Linq; namespace LambdaExpressionExample { class Program { static void Main(string[] args) { List numbers = new List { 1, 2, 3, 4, 5 }; // 람다 식을 사용하여 짝수만 선택 List evenNumbers = numbers.Where(n => n % 2 == 0).ToList(); // 결과 출력 foreach (var number in evenNumbers) { Console.WriteLine(number); } } } } 이 예제에서는 n..
예외 처리 C#에서 예외 처리를 사용하는 예시는 다음과 같습니다: using System; namespace ExceptionHandlingExample { class Program { static void Main(string[] args) { int[] numbers = { 1, 2, 3 }; try { // 인덱스가 배열의 범위를 벗어난 경우 예외 발생 Console.WriteLine(numbers[3]); } catch (IndexOutOfRangeException ex) { Console.WriteLine("Exception caught: " + ex.Message); } finally { Console.WriteLine("Finally block executed"); } Console.WriteLine(..
제네릭 C#에서 제네릭을 사용하는 예시는 다음과 같습니다: using System; namespace GenericExample { class Program { static void Main(string[] args) { // 정수형 인스턴스 생성 MyGenericClass myInt = new MyGenericClass(10); myInt.ShowType(); // 문자열형 인스턴스 생성 MyGenericClass myString = new MyGenericClass("Hello"); myString.ShowType(); } } class MyGenericClass { private T genericMember; public MyGenericClass(T value) { genericMember = value..
인터페이스 C#에서 인터페이스를 사용하는 예시는 다음과 같습니다: using System; namespace InterfaceExample { class Program { static void Main(string[] args) { Rectangle rect = new Rectangle(10, 5); Console.WriteLine("Area of Rectangle: " + rect.Area()); } } interface IShape { double Area(); } class Rectangle : IShape { private double length; private double width; public Rectangle(double length, double width) { this.length = lengt..
다형성 C#에서 다형성을 사용하는 예시는 다음과 같습니다: using System; namespace PolymorphismExample { class Program { static void Main(string[] args) { Animal myAnimal = new Animal(); Animal myPig = new Pig(); Animal myDog = new Dog(); myAnimal.AnimalSound(); myPig.AnimalSound(); myDog.AnimalSound(); } } class Animal { public virtual void AnimalSound() { Console.WriteLine("The animal makes a sound"); } } class Pig : Anima..
상속 C#에서 상속을 사용하는 예시는 다음과 같습니다: using System; namespace InheritanceExample { class Program { static void Main(string[] args) { // 부모 클래스의 객체 생성 Animal animal = new Animal(); animal.Eat(); // animal.Run()은 호출할 수 없습니다. // 자식 클래스의 객체 생성 Dog dog = new Dog(); dog.Eat(); dog.Run(); } } // 부모 클래스 class Animal { public void Eat() { Console.WriteLine("Eating..."); } } // 자식 클래스 class Dog : Animal { public vo..