본문 바로가기
728x90

분류 전체보기198

[이것이 C#이다] Chapter07: 연습문제 1. 클래스와 객체, 인스턴스는 서로 어떤 점이 다른가요? 클래스는 객체를 만들기 위한 설계도, 객체는 클래스를 실체화한 것으로 인스턴스의 한 종류이다. 예) "자동차"라는 클래스가 있고, 이 클래스는 자동차의 속성(색상, 브랜드 등)과 동작(가속, 제동 등)을 정의한다. 이 클래스를 기반으로 "블루 토요타 자동차"와 "레드 현대 자동차"와 같은 객체를 생성할 수 있다. 이렇게 생성된 객체들은 클래스의 인스턴스이며, 클래스에 정의된 속성과 동작을 가지게 된다. 2. 다음 코드에서 오류를 찾고, 오류의 원인을 설명하세요. class A { } class B: A { } class C { public static void Main() { A a = new A(); B b = new B(); A c = new.. 2023. 8. 17.
[C#] 깊은복사 얕은복사, readonly, 구조체, 튜플 깊은 복사/얕은 복사 얕은 복사(Shallow Copy): 동일한 메모리 주소를 고유함 -> 같이 변함 using System; class Program { static void Main() { int[] originalArray = { 1, 2, 3 }; int[] shallowCopyArray = (int[])originalArray.Clone(); shallowCopyArray[0] = 99; Console.WriteLine("Original Array: " + string.Join(", ", originalArray)); // 출력: Original Array: 1, 2, 3 Console.WriteLine("Shallow Copy Array: " + string.Join(", ", shallow.. 2023. 8. 17.
[C#] 분할 클래스, 확장 메서드 분할 클래스 (partial) 하나의 클래스를 앞에 partial 키워드를 붙여서 나눠서 관리하는 것이다. partial class MyClass { public void MethodPart1() { Console.WriteLine("Method from Part 1"); } } partial class MyClass { public void MethodPart2() { Console.WriteLine("Method from Part 2"); } } class Program { static void Main() { MyClass myClass = new MyClass(); myClass.MethodPart1(); // 출력: "Method from Part 1" myClass.MethodPart2(); .. 2023. 8. 17.
[C#] 상속, 다형성, 오버라이딩 상속: base 키워드 부모 클래스의 생성자나 멤버에 접근할 때 사용할 수 있다. class Parent { public void SomeMethod() { Console.WriteLine("Parent's method"); } } class Child : Parent { public void CallParentMethod() { base.SomeMethod(); // 부모 클래스의 메서드 호출 } } 위 예시는 base가 없어도 잘 작동한다. (부모 클래스로부터 상속받았기 때문에) 하지만 가독성을 위해 base를 붙여주기도 한다. 만약 오버라이딩을 한다면 꼭 base 키워드로 부모 클래스의 메서드와 파생 클래스의 메서드를 구분해줘야 한다. class Parent { public virtual void .. 2023. 8. 17.
[이것이 C#이다] Chapter06: 연습문제 1. Square() 메소드를 구현해 프로그램을 완성하세요. Square() 함수는 매개변수를 제곱하여 반환합니다. using System; namespace Chapter06Practice class MainApp { static double Square(double x) { return x * x; } static void Main(string[] args) { Console.Write("수를 입력하세요: "); string str = Console.ReadLine(); if (!double.TryParse(str, out double d) && !int.TryParse(str, out int i)) { Console.WriteLine("정수 혹은 실수만 입력해주세요"); } else { double.. 2023. 8. 16.
[C#] 참조 매개변수 전달, 여러 개의 값return, 가변 개수의 인수, 선택적 인수 참조에 의한 매개변수 전달 보통 다른 메서드에 인수를 전달할 때, 데이터가 복사되어서 전달된다. 만약 주소값을 보내고 싶다면 앞에 ref를 붙여주면 된다. class Program { public static void Main(string[] args) { int x = 3; int y = 4; Console.WriteLine($"x = {x}, y = {y}"); Swap(x, y); Console.WriteLine($"x = {x}, y = {y}"); SwapRef(ref x, ref y); Console.WriteLine($"x = {x}, y = {y}"); } static void Swap(int a, int b) { int temp = b; b = a; a = temp; } static vo.. 2023. 8. 16.
[이것이 C#이다] Chapter05: 연습문제 1. 다음과 같은 결과를 출력하는 프로그램을 for문을 이용하여 작성하세요. * ** *** **** ***** for (int i=1; i 2023. 8. 16.
[C#] switch문과 switch식 switch문 switch문은 아래와 같이 case에 맞는 값을 지정해 줄 수 있다. int score = 95; string grade = ""; int score = (int)(Math.Truncate(input/10.0) * 10; //1의 자리를 버림 switch (score) { case 90: grade = "A"; break; case 80: grade = "B"; break; case 70: grade = "C"; break; case 60: grade = "D"; break; default: grade = "F"; } switch 식 switch문은 break;나 case: 등 반복되는 코드가 많은 것을 볼 수 있다. 이때, switch 식을 사용하면 코드의 길이를 줄이고 가독성을 높일 .. 2023. 8. 16.
728x90

"); wcs_do();