1) 오류 제어(예외 처리), 런타임 에러
int a, b;
float c = 0;
try
{
a = int.Parse(Console.ReadLine());
b = int.Parse(Console.ReadLine());
c = a / b;
}
catch (FormatException eObj)
{
Console.WriteLine(eObj);
Console.WriteLine("문자를 입력하셨습니다.");
Environment.Exit(0);
}
catch (Exception eObj)
{
Console.WriteLine(eObj);
Console.WriteLine("변수의 값이 올바르지 않습니다.");
Environment.Exit(0);
}
finally
{
Console.WriteLine("프로그램이 실행 되었습니다.");
}
Console.WriteLine("A/B 값은 {0}입니다.", c);
cf. Environment.Exit(0);
- 0 : 정상종료
- 다른 숫자 : 비정상 종료
while(true)
{
int a, b;
float c = 0;
try
{
Console.Write("첫번째 숫자를 입력해주세요 : ");
a = int.Parse(Console.ReadLine());
Console.Write("두번째 숫자를 입력해주세요 : ");
b = int.Parse(Console.ReadLine());
c = a / b;
}
catch (FormatException eObj)
{
Console.WriteLine(eObj);
Console.WriteLine("문자를 입력하셨습니다.");
continue;
}
catch (Exception eObj)
{
Console.WriteLine(eObj);
Console.WriteLine("변수의 값이 올바르지 않습니다.");
continue;
}
finally
{
Console.WriteLine("프로그램이 실행 되었습니다.");
}
Console.WriteLine("A/B 값은 {0}입니다.", c);
break;
}
2) 메서드
public class A
{
public static void MethodA() // 정적메서드
{
Console.WriteLine("MethodA() in class A");
}
}
public class B
{
static void Main(string[] args)
{
A.MethodA();
}
}
public class B
{
public static void MethodA()
{
Console.WriteLine("메서드 A입니다.");
}
public static void MethodB()
{
MethodA();
Console.WriteLine("메서드 B입니다.");
MethodA();
}
static void Main(string[] args)
{
MethodB();
MethodA();
}
}
public static ulong Factorial(ulong number)
{
if(number <= 1)
{
return 1;
}
else
{
return number * Factorial(number - 1);
}
}
static void Main(string[] args)
{
ulong nfact = Factorial(5);
Console.WriteLine("5 * 4 * 3 * 2 * 1 = " + nfact);
}
public class Example
{
public static int classvari1 = 0; // 클래스 소속
public int classvari2 = 0; // 객체 소속
public Example()
{
classvari1++;
classvari2++;
}
public void Print()
{
Console.WriteLine("classvari1 : " + classvari1);
Console.WriteLine("classvari2 : " + classvari2);
}
static void Main(string[] args)
{
Example e1 = new Example();
e1.Print();
Example e2 = new Example();
e2.Print();
}
}
cf. 지역변수, 멤버 변수
지역변수는 초기화를 안 시켜주면 에러가 나며 클래스의 멤버 변수는 자리가 0으로 초기화된 후 할당이 되기 때문에 초기화를 따로 시켜주지 않더라도 0이 들어있다.
3) 참조
public class ParamValue
{
public void Increase(int n)
{
n++;
}
static void Main(string[] args)
{
int i = 10;
ParamValue pv = new ParamValue();
Console.WriteLine("호출 전 : {0}", i);
pv.Increase(i);
Console.WriteLine("호출 후 : {0}", i);
}
}
public class ParamRef
{
public int myVal = 10;
}
public class ParamTest
{
public static void Increase(ParamRef varRef)
{
varRef.myVal++;
}
public static void Main(string[] args)
{
ParamRef pr = new ParamRef();
Console.WriteLine("호출 전 : {0}", pr.myVal);
ParamTest.Increase(pr);
Console.WriteLine("호출 후 : {0}", pr.myVal);
}
}
public class ParamValue
{
public void Increase(ref int n) // ref : 변수에 대한 별칭을 만들 수 있음
{
n++;
}
public static void Main()
{
int i = 10;
ParamValue pv = new ParamValue();
Console.WriteLine("호출 전 : {0}", i);
pv.Increase(ref i);
Console.WriteLine("호출 후 : {0}", i);
}
}
public class paramRef
{
public int myVal = 10;
}
public class ParamTest
{
public static void Increase(ref paramRef varRef)
{
varRef.myVal++;
}
public static void Main(string[] args)
{
paramRef pr = new paramRef();
Console.WriteLine("호출 전 : {0}", pr.myVal);
ParamTest.Increase(ref pr);
Console.WriteLine("호출 후 : {0}", pr.myVal);
}
}