728x90

함수 형식

returnType FunctionName ( parameterType parameter1, parameterType parameter2 = "default" )
{
  print(parameter1);
  print(parameter2);
  print("FunctionName 첫글자는 대문자");
  print("디폴트 값을 가지는 파라메터는 맨 뒤에 위치한다.");
  
  return data;
}

 

 

접근제한자 

 

[ private, public, protected ] 

default : private 

 

public :  모든 레벨에서 접근 가능 

private : 동일한 Class에서만 접근 가능

protected : 동일 Class 또는 상속받는 Class에서만 접근 가능 

 

반복문 

 

int i;
int j = 3;
for (i = 0, Console.WriteLine($"Start: i={i}, j={j}"); i < j; i++, j--, Console.WriteLine($"Step: i={i}, j={j}"))
{
    //...
}
// Output:
// Start: i=0, j=3
// Step: i=1, j=2
// Step: i=2, j=1

initializer에서 loop 변수를 선언하지 않는 경우 initializer 섹션에서 0개 이상의 식을 사용할 수 있다. 


int j = 3;
for (int i = 0; i < j; i++, j--, Console.WriteLine($"Step: i={i}, j={j}"))
{
    //...
}
// Output:
// Step: i=1, j=2
// Step: i=2, j=1


i와 j 두가지 변수 모두 값을 변경할 수 있다.

 

foreach

int[,] numbers = {{1, 2, 3, 4, 5},{0, 1, 2, 3, 4}};
foreach (int number in numbers)
{
    Console.Write(number+" ");
}
// Output:
// 1 2 3 4 5 0 1 2 3 4

 

 

컬렉션 

 

ArrayList

ArrayList 콜렉션은 non generic collection으로 타입을 정해두지 않고 여러 타입의 데이터를 동시에 담을 수 있다.

ArrayList arrayList = new ArrayList();
arrayList.Add("stringValue");
arrayList.Add(1);
arrayList.Add(false);

print(arrayList[0]);

// Output:
// stringValue

 

 

List<T> 

List<T> 콜렉션은 타입을 지정하여 동일한 타입의 데이터들을 담을 수 있다. 

 

ArrayList보다는 List<T> 클래스를 사용할 것을 권장하고 있다. 

 

 

HashTable / Dictionary<K, V>

Key Value 형식으로 데이터를 저장하며 Key와 Value의 타입은 동일하지 않을 수 있다.

HashTable hashTable = new HashTable();
hashTable.Add("일",1);
hashTable.Add(2,"이");

print(hashTable["일"]);
print(hashTable[2]);

 

Dictionary도 HashTable과 같지만 제네릭 지정하여 사용하기 때문에 List<T>와 마찬가지로 성능이 더 좋다.

 

Queue<T> / Stack<T>

선입선출 FIFO / 후입선출 LIFO

Queue<int> q = new Queue<int>();
q.Enqueue(1);
q.Enqueue(2);

for (; q.Count != 0; q.Dequeue())
{
    Console.WriteLine(q.Peek());
}
// Output:
// 1 2

Stack<int> st = new Stack<int>();
st.Push(1);
st.Push(2);

for(; st.Count != 0;)
{
    Console.WriteLine(st.Pop());
 }
 // Output:
 // 2 1

 

 

namespace

namespace 키워드를 사용하여 관련 파일 혹은 다른 namespace 등을 포함하는 범위를 선언할 수 있다. 

 

선언한 namespace는 using 키워드를 통해 사용할 수 있고

namespace에 alias 를 부여하여 사용할 수 있다.

 

namespace Utils
{
    struct UtilStruct { }

    enum UtilEnum { a, b }

    delegate void UtilDelegate(int i);

    namespace Calendar
    {
        static class MicroTimer { }

        interface Timer { }
    }
}

global using <fully-qualified-Utils>;
using Alias = Utils.Calendar;

 

global using 키워드는 프로젝트내의 모든 파일에서 using 이 적용된다는 것을 의미한다.

'C#' 카테고리의 다른 글

C# 열거형  (0) 2024.04.16

+ Recent posts