달력

6

« 2025/6 »

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
2016. 4. 9. 20:31

정리 프로그래밍/C#2016. 4. 9. 20:31

enum

1. 열겨형 형식

2. 상수 집합

3. 첫 번째 열거자 값은 0이고 1씩 증가

4. public enum Operators { Add, Sub, Multi, Divide }


클래스 생성자

1. 무조건 public 으로 선언

2. 리턴 타입 명시하지 않음

3. 클래스명과 같아야 함.


문자열로 변환

1. 모든 객체는 Tostring() 메소드를 가지고 있다.


private

1. 외부에서 접근 불가, 첫글자에 밑줄(_)을 쳐서 구분하기 쉽게 할것


partial

클래스 등을 두 이상의 소스 파일로 분할 가능.


속성

1. 보호

2. 메서드처럼 코딩 가능

3. get, set

4. 매개변수는 한개만 가능, 'value' 키워드는 속성의 타입과 같아야 함.

5. 쓰기 보호를 위해서 set을 빼면 읽기전용이 됨. 


함수 오버로딩

1. 함수명은 같더라도 매개변수가 다르면 다른 함수로 인식


DateTime

1. DateTime date = new DateTime(2016, 4, 9)

2. DateTime current = DateTime.Now


배열 선언 방법 3가지

사이즈가 지정되는 고정배열

1. int[] numberArray = new int[5];

   numberArray[0] = 1;

   numberArray[0] = 2;

   numberArray[0] = 3;

   numberArray[0] = 4;

   numberArray[0] = 5;

2. int[] numberArray2 = new int[] { 1, 2, 3, 4, 5 };

3. int[] numberArray3 = { 1, 2, 3, 4, 5 };


클래스 배열 선언

1. Customer[] customerArray = new Customer[5];

   customerArray[0] = new Customer("first", "last", new DateTime(2000,1,1));

   customerArray[1] = new Customer("first", "last", new DateTime(2000,1,1));

   customerArray[2] = new Customer("first", "last", new DateTime(2000,1,1));

   customerArray[3] = new Customer("first", "last", new DateTime(2000,1,1));

   customerArray[4] = new Customer("first", "last", new DateTime(2000,1,1));

2. Customer[] customerArray2 =

   {

      new Customer("first", "last", new DateTime(2000,1,1));

      new Customer("first", "last", new DateTime(2000,1,1));

      new Customer("first", "last", new DateTime(2000,1,1));

      new Customer("first", "last", new DateTime(2000,1,1));

      new Customer("first", "last", new DateTime(2000,1,1));

   };


반복문 for

for (int index = 1; index <= 10; index++)

{

sum += index;

}


internal class Customer (기본값)

같은 네임스페이스 안에서만 클래스만 사용


public class Customer

다른 네이스페이스 안에서도 클래스를 사용 가능


ArrayList 형

1. 동적 배열(다른 타입도 가능)

2. 링크드 리스트로 삽입 삭제시 자동으로 처리

3. using System.Collections

4. 자동으로 형변환


ArrayList arrayList = new ArrayList();

arrayList.Add(0);

arrayList.Add(1);

arrayList.Add(2);

arrayList.Add("HI");  // 타입이 달라지면 비효율, 그냥 변수로 처리하는게 나음

arrayList.Add(new Customer());  // 타입이 달라지면 비효율


arrayList.Insert(2, 2) // 2번째에 2를 삽입

arrayList.Remove(2) // 2를 찾아 지움

arrayList.RemoveAt(1) // 1번째 값 지움


int sum = 0;

for (int index = 0; index < arrayList.Count; index++)

{

    int num = (int)arrayList[index];  // 형 변환이 필요 (많으면 많을수록 성능 저하)

    sum += num;

}


List<T>

리스트 클래스, 데이터 타입이 지정된 동적 배열 (일반화 클래스, 제네릭 클래스, 일반화 컬렉션, 제네릭 컬렉션)

T는 데이터 타입을 나타냄


int[] intArray = new int[5];

ArrayList arrayList = new ArrayList();

List<int> intList = new List<int>();


List<int> intList = new List<int>();

int sum2 = 0;

for (int index = 0; index < intList.Count; index++)

{

    int value =intList[index];  // 형 변환이 필요 없음

    sum2 += num;

}


foreach (int value in intList)  // 위 for보다 깔끔하고 효율적.

{

    sum += value;

}


줄바꿈

Environment.NewLine;


virtual

가상 메서드

자식 클래스에서 재정의된 메서드가 존재하면 그걸 호출함


override

자식 클래스에서 재정의 할 때 붙임


public 이나 protected 만 자식클래스에서 사용 가능

private은 불가

자식 생성자가 호출 될 때 부모 생성자의 매개변수가 없은 생성자가 호출됨

base 키워드를 사용해서 매개변수를 적어준다.

부모 클래스의 변수에 자식 클래스의 변수가 대입될 수 있다. 그러면 재정의됨 메서드는 자식 클래스의 메서드가 호출됨.


String.IsNullOrEmpty()

변수가 null 이거나 빈 경우

if (!String.IsNullOrEmpty(prefix))


이스케이프 문자 사용

"{0}\\{1}\\"

@"{0}\{1}\"

path = String.Format(@"{0}\", DateTime.Now.Year)

https://msdn.microsoft.com/ko-kr/library/8kb3ddd4.aspx


try

try { }

catch (Exception ex)

{ }


폴더 관련 함수

using System.IO;

1. 폴더 생성: Directory.CreateDirectory(path)

2. 폴더 확인: Directory.Exists(path)

3. 경로 합침: Path.Combine(path, name)


파일 쓰기

using (StreamWriter writer = new StreamWriter(path, true))  // true는 append 여부
{
     writer.Write(data);
}


확장 메소드

1. 항상 static 클래스에 static 펑션으로 선언되어야 함.

2. 첫번째 인자는 this 키워드에 확장할 타입이 와야 함.

3. 확장 메소드는 아래 화살표가 붙음



'프로그래밍 > C#' 카테고리의 다른 글

C# 키워드 O~S  (0) 2016.05.31
C# 키워드 F~N  (0) 2016.05.21
C# 키워드 A-E  (0) 2016.05.13
연산자 2  (0) 2016.05.10
C# 총정리  (0) 2016.04.19
:
Posted by 지훈2