티스토리 뷰
Object Initializer 사용법을 설명하는 샘플코드이다.
using System;
namespace Sample
{
class Program
{
static void Main(string[] args)
{
Line L1 = new Line();
Point myP1 = new Point();
myP1.X = 1;
myP1.Y = 2;
Point myP2 = new Point();
myP2.X = 3;
myP2.Y = 4;
L1.P1 = myP1;
L1.P2 = myP2;
Console.WriteLine(L1.ToString());
Line L2 = new Line();
Point itsP1 = new Point { X = 5, Y = 6 }; // Object Initializers
Point itsP2 = new Point { X = 7, Y = 8 }; // Object Initializers
L2.P1 = itsP1;
L2.P2 = itsP2;
Console.WriteLine(L2.ToString());
// Object Initializers
Line L3 = new Line
{
P1 = new Point { X = 11, Y = 12 },
P2 = new Point { X = 13, Y = 14 }
};
Console.WriteLine(L3.ToString());
}
}
public class Line
{
Point p1;
public Point P1
{
get { return p1; }
set { p1 = value; }
}
Point p2;
public Point P2
{
get { return p2; }
set { p2 = value; }
}
public override string ToString()
{
return String.Format("({0}, {1}) - ({2}, {3})", P1.X, P1.Y, P2.X, P2.Y);
}
}
public class Point
{
int x;
public int X
{
get { return x; }
set { x = value; }
}
int y;
public int Y
{
get { return y; }
set { y = value; }
}
public override string ToString()
{
return String.Format("({0}, {1})", X, Y);
}
}
}
namespace Sample
{
class Program
{
static void Main(string[] args)
{
Line L1 = new Line();
Point myP1 = new Point();
myP1.X = 1;
myP1.Y = 2;
Point myP2 = new Point();
myP2.X = 3;
myP2.Y = 4;
L1.P1 = myP1;
L1.P2 = myP2;
Console.WriteLine(L1.ToString());
Line L2 = new Line();
Point itsP1 = new Point { X = 5, Y = 6 }; // Object Initializers
Point itsP2 = new Point { X = 7, Y = 8 }; // Object Initializers
L2.P1 = itsP1;
L2.P2 = itsP2;
Console.WriteLine(L2.ToString());
// Object Initializers
Line L3 = new Line
{
P1 = new Point { X = 11, Y = 12 },
P2 = new Point { X = 13, Y = 14 }
};
Console.WriteLine(L3.ToString());
}
}
public class Line
{
Point p1;
public Point P1
{
get { return p1; }
set { p1 = value; }
}
Point p2;
public Point P2
{
get { return p2; }
set { p2 = value; }
}
public override string ToString()
{
return String.Format("({0}, {1}) - ({2}, {3})", P1.X, P1.Y, P2.X, P2.Y);
}
}
public class Point
{
int x;
public int X
{
get { return x; }
set { x = value; }
}
int y;
public int Y
{
get { return y; }
set { y = value; }
}
public override string ToString()
{
return String.Format("({0}, {1})", X, Y);
}
}
}
'Code > C#' 카테고리의 다른 글
RichTextBox AutoResize 샘플코드 (0) | 2008.04.08 |
---|---|
iTextSharp 샘플코드 - Hello World (0) | 2008.04.08 |
[C#] 간단한 Object Initializer 샘플 코드 (0) | 2008.02.11 |
[C#] 간단한 Lambda Expression 샘플 코드 (0) | 2008.02.11 |
[C#] Lambda Expression vs Anonymous Method (0) | 2008.02.11 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- ScreenHunter
- jre
- java
- 유틸리티
- tagREADYSTATE
- AdSense감추기
- c#
- 애드센스감추기
- AxWebBrowser
- AdSense숨기기
- Rollback Rx
- Automation
- windows
- 애드센스숨기기
- iText
- Regular Expressions
- Phalanger
- download.com
- 스크린캡쳐
- registry
- autohotkey
- Service pack
- READYSTATE_COMPLETE
- Microsoft
- .net framework
- Sample Code
- iTextSharp
- WinAutomation
- DotNetMagic
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함