CS/C# 12

20240201 C# - Template

using System; internal class Program { // 템플릿(Template) // 제네릭(Generic) 일반화 // 일반화 프로그래밍 /*public static int Sum(int _lhs, int _rhs) { return _lhs + _rhs; } public static float Sum(float _lhs, float _rhs) { return _lhs + _rhs; }*/ // Function Template / Method Template public static void PrintType(T1 _val1) { // PrintType(T1 _val1, T2 _val2) 식으로 여러개 쓸 수도 있고 G1, T2, A처럼 T말고 다른거 써도 됨 Console.Write..

CS/C# 2024.02.01

20240131 C# - Inheritance 상속과 Factory패턴 (정리 필요)

Program.cs using System; using System.Runtime.CompilerServices; class Program { // 추상 함수 전용 추상 클래스 interface IInterface { // interface는 다중상속이 가능함 //int ival = 100; // interface는 값을 가질 수 없음 public void Foo(); // Overriding 해야하는 함수는 반드시 private일 수 없음 protected void Bar(); // interface는 접근지정자를 안적어두면 기본으로 public이 됨 // Foo, Bar 함수를 임시로 만들어 쓸 때 주로 사용되는 이름 } // 상속(Inheritance) // 부모 - 자식 // Parent - Ch..

CS/C# 2024.01.31

20240130 C# - Struct 구조체 (정리 필요)

using System; class StructExample { // 구조체를 쓸 일이 있으면 보통 따로 파일을 만드는데 해당 클래스에서만 쓸 때는 해당 클래스 안에 만들기도 한다 // 또 해당 클래스를 사용할 일이 있을 때 구조체 파일이 나눠져있으면 클래스 파일과 구조체 파일 둘 다 들고 와야함 public struct Structure { private int x = 0; // Structure가 public이여도 멤버변수가 public이 아니면 바깥에서 변수에 접근할 수 없음 private int y = 0; // public으로 멤버변수를 만들거나 Getter/Setter를 만들어줘야함 public Structure(int _x, int _y) { // 생성자 x = _x; y = _y; Cons..

CS/C# 2024.01.30

20240129 C# - Class

program.cs using System; class Program { // C에서 static - 정적 변수. 지역 변수지만 지역을 벗어나도 값을 저장, 접근은 지역 내에서만 가능. 저장 공간이 전역변수와 같은 곳이기 때문 static void Test() { ClassExample ce = new ClassExample(); } static void Main(string[] _args) { // Object, Instance int intValue = 50; //ClassExample example = new ClassExample(intValue); ClassExample example = new ClassExample(); // ClassExample의 객체 생성. 인스턴스화 하다. 동적할당 /..

CS/C# 2024.01.29

20240126 C# - String

using System;using System.Text;internal class Program { public static void Main(string[] _args) { char c = 'a'; char[] charStr = new char[] { 'H', 'e', 'l', 'l', 'o' }; // c의 동적 할당 malloc c#의 동적할당 new // new int(); // 생성자(Constructor) // new를 사용하면 생성자가 자동으로 호출됨 new int 뒤에 괄호를 써야하는 이유. 생성자는 함수이기 때문에 매개변수가 들어가는 괄호를 넣어줘야함 // 생성자 ..

CS/C# 2024.01.26

20240125 C# - Array

using System; class Program { // ref: Reference, 참조 함수 내부에서 외부의 값을 바꿈 안바꿔도 문제 없음 // 포인터: 주소를 저장하는 공간이 새로 생김 // Reference: 같은 변수에 이름이 하나 더 생김 intVal = _val // ref int _val로 받으면 그 변수는 참조해서 사용하겠다 /// /// 함수 위에 /// 치면 함수 설명을 적을 수 있다 /// 함수 위에 마우스 오버하면 보임 /// /// 매개변수 설명 함수에 넣은 값 설명을 볼 수 있음 private static void ChangeValue(ref int _val) { _val = 100; } // ref과 다르게 out은 함수 외부에서 들어온 변수의 값을 무조건 바꿔야 함(대입 ..

CS/C# 2024.01.25
반응형