[C#기초] 1. 데이터형 및 문법 기초

INDEX

  1. 데이터형
  2. 타입캐스팅
  3. 제어문
  4. 배열
  5. 클래스 배열
  6. 배열을 함수로 전달
  7. string 토큰화

데이터형

타입캐스팅

int x = 3;
float y = 4.5f;
string z;

x = int.parse(y);
x = Convert.ToInt32(y);
z = y.ToString()

제어문

switch case

for(;;)

foreach

점프문

예외처리문

try ~ catch ~ finally

try

try
{
    //예외가 발생할 수 있는 코드
}catch (예외처리객체 e)
{
    //예외처리
} finally
{
   //항상 실행되는 코드
}
try
{
     array[1025] = 10;
}catch (IndexOutOfRangeException e)
{
    Console.WriteLine("배열 인덱스 에러 발생");
    //예외처리
} 

throw

if(x >= num.Length)
{
    throw new IndexOutOfRangeException();
}

배열

‘배열’

int[][] array = new int[][]
{
    new int [] {1,2,3},
    new int [] {4,5,6},
    new int [] {7,8,9}
};
int[][] array = {
    new int [] {1,2,3},
    new int [] {4,5,6},
    new int [] {7,8,9}
    };

클래스 배열

형식

class A
{
    public int number;
}
A[] TestArray = new A[3];
TestArray[0] = new A();
TestArray[0].number = 12;

int result = TestArray[0].number;

배열을 함수로 전달

1차원 배열을 함수로 전달

int[] array= {1,2,3,4};
void func(int[] arr){}
func (array);

2차원 배열을 함수로 전달

string 토큰화