[C#] 2. 언어구조

Posted by seunggwon
2008. 12. 14. 21:12 IT Note/기타언어
명칭
using System;
class IdentifierApp
{
public static void Main()
{
int MyVar = 1, myvar = 2;
int @int = 10, 변수 = 20;
Console.WriteLine("Myvar = " + MyVar + ", myvar = " + myvar);
Console.WriteLine("@int = " + @int + ", 변수 = " + 변수);
}
}
실행결과
Myvar = 1, myvar = 2
@int = 10, 변수 = 20


정수형상수
using System;
class IntegerConstantApp
{
public static void Main()
{
int i = 255, h = 0Xff;
long l = 0XffL;
Console.WriteLine("i = {0}, h = {1}", i, h);
if (h == 1)
{
Console.WriteLine("Yes");
}
else
{
Console.WriteLine("No");
}
}
}
실행결과
i = 255, h = 255
No


실수형 상수

'IT Note > 기타언어' 카테고리의 다른 글

[C#] 1. C#의 개요  (0) 2008.12.13
[MFC] Menu(2)  (0) 2008.11.24
[MFC] Menu(1)  (0) 2008.11.24

[C#] 1. C#의 개요

Posted by seunggwon
2008. 12. 13. 21:39 IT Note/기타언어
콘솔 애플리케이션 
using System;
class HelloWorld
{
public static void Main()
{
Console.WriteLine("Hello World!");
}
}
실행결과 : Hello World!


윈폼 애플리케이션
using System;
using System.Windows.Forms;
class WinFormApp : Form
{
WinFormApp()
{
Text = "Hello World!";
}
public static void Main()
{
Application.Run(new WinFormApp());
}
}
실행결과



자료형
using System;
class PrimitiveTypeApp
{
public static void Main()
{
bool b;
int i = int.MaxValue;
uint j = uint.MaxValue;
double d = double.MaxValue;
b = (i != 0);
Console.WriteLine("boolean b = " + b);
Console.WriteLine("Max value of signed integer = " + i);
Console.WriteLine("Max value of unsigned integer = " + j);
Console.WriteLine("Max value of double = " + d);
}
}
실행결과
boolean b = True
Max value of signed integer = 2147483647
Max value of unsigned integer = 4294967295
Max value of double = 1.79769313486232E+308


스트링
using System;
class StringApp
{
public static void Main()
{
string s = "Hello";
s += " World";
Console.WriteLine(s + "!");
}
}
실행결과 : Hello World!


프로퍼티
using System;
class PropertyClass
{
private int privateValue;
public int PrivateValue
{
get
{
return privateValue;
}
set
{
privateValue = value;
}
}
public void PrintValue()
{
Console.WriteLine("Hidden Value = " + privateValue);
}
}
class PropertyApp
{
public static void Main()
{
int n;
PropertyClass obj = new PropertyClass();
obj.PrivateValue = 100;
obj.PrintValue();
n = obj.PrivateValue;
Console.WriteLine("       Value = " + n);
}
}
실행결과
Hidden Value = 100
              Value = 100



연산자 중복
using System;
class Even
{
int evenNumber;
public Even(int n) // 생성자
{
evenNumber = (n % 2 == 0) ? n : n+1;
}
public static Even operator++(Even e) // ++연산자
{
e.evenNumber += 2; // 다음 짝수
return e;
}
public static Even operator--(Even e)
{
e.evenNumber -= 2;
return e;
}
public void PrintEven()
{
Console.WriteLine("Even Number = " + evenNumber);
}
}
class OperatorOverloadingApp
{
public static void Main()
{
Even e = new Even(4);
e.PrintEven();
++e;
e.PrintEven();
--e;
e.PrintEven();
}
}
실행결과
Even Number = 4
Even Number = 6
Even Number = 4


델리게이트
using System;
delegate void SampleDelegate(); // 델리게이트 정의
class DelegateClass
{
public void DelegateMethod()
{
Console.WriteLine("In the DelegateClass.DelegateMethod...");
}
}
class DelegateApp
{
public static void Main()
{
DelegateClass obj = new DelegateClass();
SampleDelegate sd = new SampleDelegate(obj.DelegateMethod);
sd(); // invoke obj.DelegateMethod() indirectly.
}
}

실행결과 : In the DelegateClass.DelegateMethod...


이벤트
using System;
using System.Windows.Forms;
class EvenApp : Form
{
public EvenApp() // 생성자
{
this.Click += new EventHandler(ClickEvent); // 이벤트 처리기 등록
}
void ClickEvent(object sender, EventArgs e) // 이벤트 처리기 작성
{
MessageBox.Show("Hello world!");
}
public static void Main()
{
Application.Run(new EvenApp());
}
}
실행결과



스레드
using System;
using System.Threading; // 반드시 포함!
class ThreadApp
{
static void ThreadBody() // 스레드 몸체
{
Console.WriteLine("In the thread body...");
}
public static void Main()
{
ThreadStart ts = new ThreadStart(ThreadBody);
Thread t = new Thread(ts);
Console.WriteLine("*** Start of Main");
t.Start();
Console.WriteLine("*** End of Main");
}
}
실행결과
*** Start of Main
*** End of Main
In the thread body...

'IT Note > 기타언어' 카테고리의 다른 글

[C#] 2. 언어구조  (0) 2008.12.14
[MFC] Menu(2)  (0) 2008.11.24
[MFC] Menu(1)  (0) 2008.11.24