본문 바로가기
[ Program ]/C#

HELLO WORLD로 보는 C#의 세계

by 관이119 2012. 9. 18.
출처 SSEY's Blog | 쎄이
원문 http://blog.naver.com/tear230/100006940429

 

전형적인 "Hello World" 프로그램을 작성한다. 이 프로그램을 통해서 C#과 닷넷 프레임워크의 다양한 특성들을 들여다 볼 것이다.

 

1. Hello World-초보자

 

public class HelloWorld
{
public static void Main()
{
System.Console.WriteLine("HELLO WORLD");
}
}

 

2. 조금만 고쳐보자

 

using System;
public class HelloWorld
{
public static void Main()
{
Console.WriteLine("HELLO WORLD");
}
}

 

3. Command Line Arguments

 

using System;
public class HelloWorld
{
public static void Main(string[] args)
{
Console.WriteLine(args[0]);
}
}

 

4. Constructor 호출

 

using System;
public class HelloWorld
{
public HelloWorld()
{
Console.WriteLine("HELLO WORLD");
}
public static void Main()
{
HelloWorld hw = new HelloWorld();
}
}

 

5. 보다 객체지향(OO)적으로

 

using System;
public class HelloWorld
{
public void helloWorld()
{
Console.WriteLine("HELLO WORLD");
}

public static void Main()
{
HelloWorld hw = new HelloWorld();
hw.HelloWorld();
}
}

 

6. 다른 클래스를 호출하자

 

using System;
public class HelloWorld
{
public static void Main()
{
HelloWorldHelperClass hwh = new HelloWorldHelperClass();
hwh.writeHelloWorld();
}
}

public class HelloWorldHelperClass
{
public void writeHelloWorld()
{
Console.WriteLine("Hello World");
}
}

 

7. 상속(Inheritance)을 이용하자

 

abstract class HelloWorldBase
{
public abstract void writeHelloWorld();
}

class HelloWorld : HelloWorldBase
{
public override void writeHelloWorld()
{
Console.WriteLine("Hello World");
}
}

class HelloWorldImp
{
static void Main()
{
HelloWorldBase hwb = HelloWorld;
HelloWorldBase.writeHelloWorld();
}
}

 

8. Static Constructor

 

using System;
public class HelloWorld
{
private static string strHelloWorld;
static HelloWorld() //static생성자
{
strHelloWorld = "Hello World";
}
void writeHelloWorld()
{
Console.WriteLine(strHelloWorld);
}
public static void Main()
{
HelloWorld hw = new HelloWorld();
hw.writeHelloWorld();
}
}

 

9. Exception Handling(예외 처리)

 

using System;
public class HelloWorld
{
public static void Main(string[] args)
{
try
{
Console.WriteLine(args[0]);
}
catch(IndexOutOfRangeException e)
{
Console.WriteLine(e.ToString());
}
}
}

 

10. DLL 생성과 이용

 

using System;
namespace HelloLibrary
{
public class HelloMessage
{
public string Message
{
get
{
return "Hello, World!!!";
}
}
}
}
///////////////////////////////////////////

using System;
using HelloLibrary;
namespace HelloApplication
{
class HelloApp
{
public static void Main(string[] args)
{
HelloMessage m = new HelloMessage();
}
}
}

 

11. Property(속성) 이용

 

using System;
public class HelloWorld
{
public string strHelloWorld
{
get
{
return "Hello World";
}
}

public static void Main()
{
HelloWorld hw = new HelloWorld();
Console.WriteLine(cs.strHelloWorld);
}
}


12. Delegates(대리자) 이용

 

using System;
class HelloWorld
{
static void writeHelloWorld()
{
Console.WriteLine("HelloWorld");
}
static void Main()
{
SimpleDelegate simpledelegate = new SimpleDelegate(writeHelloWorld);
simpledelegate();
}
}

 

13. 전처리 지시문 이용

 

#define DEBUGGING

using System;
using System.Diagnostics;
public class HelloWorld : Attribute
{
[Conditional("DEBUGGING")]
public void writeHelloWorld()
{
Console.WriteLine("Hello World");
}

public static void Main()
{
HelloWorld hw = new HelloWorld();
hw.writeHelloWorld();
}
}

 

14. Interfaces(상속) 이용

 

using System;
interface IHelloWorld
{
void writeHelloWorld();
}

public class HelloWorld : IHelloWorld

{

public void writeHelloWorld()
{
Console.WriteLine("Hello World");
}

public static void Main()
{
HelloWorld hw = new HelloWorld();
hw.writeHelloWorld();
}
}

 

15. 동적인 Hello World

 

using System;
using System.Reflection;
namespace HelloWorldNS
{

public class HelloWorld
{
public string writeHelloWorld()
{
return "HelloWorld";
}

public static void Main(string[] args)
{
Type hw = Type.GetType(args[0]);

// 클래스를 동적으로 초기화 한다.

object[] nctorParams = new object[] {};
object nobj = Activator.CreateInstance(hw, nctorParams); //, nctorParams);

/*
public static object CreateInstance(
Type type,
object[] args
);
type : 만들 개체의 형식
args : 호출할 생성자의 매개 변수와 번호, 순서 및 형식이 일치하는 인수의 배열.
args가 빈 배열이거나 null 참조(Visual Basic의 Nothing)이면
매개 변수를 사용하지 않는 생성자(기본 생성자)가 호출.
*/

object[] nmthdParams = new object[] {};

string strHelloWorld = (string) hw.InvokeMember("writeHelloWorld",
BindingFlags.Default | BindingFlags.InvokeMethod,
null, nobj, nmthdParams);

Console.WriteLine(strHelloWorld);

/*

*Type.InvokeMember() : 현재 Type의 특정 멤버 호출
public object InvokeMember(
string name,
BindingFlags invokeAttr,
Binder binder,
object target,
object[] args
);

[매개 변수]
*name : 호출할 생성자, 메서드, 속성 또는 필드 멤버의 이름이 들어 있는 String
또는 기본 멤버를 호출하는 빈 문자열("").
*invokeAttr: 검색방법을 지정하는 하나이상의 BindingFlags로 구성된 비트마스크.
액세스 권한은 Public, NonPublic, Private, InvokeMethod, GetField 등과 같은
BindingFlags 중 하나. 조회 형식을 지정할 필요가 없습니다.
조회 형식을 생략하면 BindingFlags.Public | BindingFlags.Instance가 적용.
* binder : 속성 집합을 정의하고 바인딩을 활성화하는 Binder 개체.
이 개체는 또한 리플렉션을 통해 오버로드된 멤버를 선택할 수 있고
인수 형식을 강제 변환할 수 있으며 멤버를 호출할 수도 있습니다.
또는 DefaultBinder를 사용하려는 경우 null 참조(Visual Basic의 Nothing).

* target : 지정된 멤버를 호출할 Object입니다.
args : 호출할 멤버에 전달하는 인수를 포함하는 배열.

[반환 값]
호출된 멤버의 반환 값을 나타내는 Object를 반환.
*/

}
}
}


16. Unsafe Hello World

 

// unsafe 키워드는 안전하지 않은 컨텍스트를 나타내며, 포인터에 관련된 모든 작업에 필요.

// 컴파일하려면 컴파일러 옵션 /unsafe를 지정.

// 안전하지 않은 코드는 공용 언어 런타임으로 확인할 수 없다.

using System;
public class HelloWorld
{
unsafe public void writeHelloWorld(char[] chrArray)
{
fixed(char *parr = chrArray)
{
char *pch = parr;
for(int i=0; i<chrArray.Length; i++)
Console.Write(*(pch+i));
}
}

public static void Main()
{
HelloWorld hw = new HelloWorld();
char[] chrHelloWorld = new char[] {'H','e','l','l','o', ' ', 'W','o','r','l','d'};
hw.writeHelloWorld(chrHelloWorld);
}
}


17. InteropServices 이용

 

using System;
using System.Runtime.InteropServices;

// COM interop 및 플랫폼 호출 서비스를 지원하는 많은 멤버를 제공

class Class1
{
[DllImport("kernel32")]
private static extern int Beep(int dwFreq, int dwDuration);

static void Main(string[] args)
{
Console.WriteLine("Hello World");
Beep(1000, 2000);
}
}

 

자료출처 : http://cafe.naver.com/wml/48

'[ Program ] > C#' 카테고리의 다른 글

[C#자료구조]Linked-List  (0) 2012.09.18
[C#]랜덤파일명 생성하는 방법  (0) 2012.09.18
GridView에서 엑셀로 저장하기  (0) 2012.09.18
C#과 API  (0) 2012.09.18
.NET Framework의 강력한 이름 및 보안  (0) 2012.09.18

댓글