
using System;
namespace NestedClass
{
class Container
{
public class Nested // 중첩 클래스
{
private Container parent;
public Nested()
{
}
public Nested(Container parent)
{
this.parent = parent;
}
}
}
class MainApp
{
static void Main(string[] args)
{
Container.Nested nest = new Container.Nested();
}
}
}
parital 클래스 {}
using System;
namespace PartialClass
{
partial class MyClass
{
public void Metod1()
{
Console.WriteLine("Method1");
}
public void Metod2()
{
Console.WriteLine("Method2");
}
}
partial class MyClass
{
public void Metod3()
{
Console.WriteLine("Method3");
}
}
class MainApp
{
static void Main(string[] args)
{
MyClass obj = new MyClass();
obj.Metod1();
obj.Metod2();
obj.Metod3();
}
}
}
[실행 결과]
Method1
Method2
Method3
static 클래스 이름 {
static 메서드 이름 (this 대상 형식 식별자, ...) {} }
using MyExtension;
using System;
namespace MyExtension
{
public static class IntegerExtension // static 수식
{
public static int Power(this int myInt, int exponent) // static 수식, this 키워드
{
int result = myInt;
for (int i = 1; i < exponent; i++)
result = result * myInt;
return result;
}
}
}
namespace ExtensionMethod
{
class MainApp
{
static void Main(string[] args)
{
Console.WriteLine($"3^4: {3.Power(4)}");
Console.WriteLine($"2^10: {2.Power(10)}");
}
}
}
[실행 결과]
3^4: 81
2^10: 1024
using MyExtension;
namespace MyExtension
{
public static class StringExtension
{
public static string Append(this string myStr, string input)
{
string result = myStr + input;
return result;
}
}
}
namespace ExtensionMethod
{
class MainApp
{
static void Main(string[] args)
{
string hello = "Hello";
Console.WriteLine(hello.Append(" Fruit!"));
}
}
}
[실행 결과]
Hello Fruit!
▪ 참고: Hello Fruit! - 클래스