Code/C# (72) 썸네일형 리스트형 iTextSharp 샘플코드 - Hello World iTextSharp 컴포넌트를 이용한 샘플코드이다. 가장 기본적인 예제로 Hello World 파일을 만드는 예제이다. using System; using System.IO; using iTextSharp.text; using iTextSharp.text.pdf; namespace iTextSharpSample { class Program { static void Main(string[] args) { // step 1: // Create an instance of the iTextSharp.text.Document object Document document = new Document(); // step 2: // Create a Writer that listens to this document and.. [C#] Object Initializer 사용법 샘플 코드 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 i.. [C#] 간단한 Object Initializer 샘플 코드 Object Initializer를 이용하는 간단한 샘플코드이다. 아래에서 P1과 P2의 property에 값을 대입하는 방식을 살펴보라. using System; namespace Sample { class Program { static void Main(string[] args) { Point P1 = new Point(); P1.X = 2; P1.Y = 3; Console.WriteLine(P1.ToString()); // Object Initializers Point P2 = new Point { X = 7, Y = 8 }; Console.WriteLine(P2.ToString()); } } public class Point { int x; public int X { get { return x; .. [C#] 간단한 Lambda Expression 샘플 코드 간단한 Lambda Expression 샘플 코드이다. Lambda Expression에서 return 값을 살펴보자. using System; namespace LambdaSample { class Program { delegate int Action(int number); static void Main(string[] args) { Action action1 = number => number + 2; Action action2 = number => { return number + 2; }; int i = action1(3); int j = action2(3); Console.WriteLine(i); Console.WriteLine(j); } } } [C#] Lambda Expression vs Anonymous Method Anonymous Method와 Lambda Expression을 비교해보자. Lambda Expression이 조금 더 단순해보인다. using System; namespace DelegateSample { class Program { static void Main(string[] args) { Human Tom = new Human(); // Anonymous Method Tom.ActionDelegate += delegate(string text) { Console.WriteLine("Anonymous:" + text); }; // Lambda Expression Tom.ActionDelegate += (string text) => Console.WriteLine("Lambda1:" + text);.. [C#] Extension Methods 사용법 Extension Methods 사용법은 2가지이다. 기존의 static method 사용법과 동일하게 쓰는 방법도 가능하다. 아래 예제는 exception 발생없이 잘 실행된다. Extension methods are static methods. Extension methods can only be declared in static classes. using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string s = "123"; int i = s.ToInt32(); int j = Extensions.ToInt32(s); Console.WriteLine(i); Console.Write.. [C#] const vs readonly const 대 readonly 비교 using System; namespace Test { class Program { static void Main(string[] args) { Some s1 = new Some(); Console.WriteLine(Some.constField); // const : 항상 static Console.WriteLine(s1.readonlyField1); Console.WriteLine(s1.readonlyField2); Some s2 = new Some(5); Console.WriteLine(s2.readonlyField2); } } class Some { // const readonly 공통 : 클래스 밖에서 값을 바꿀 수 없음 // const : 항상 static (.. PInvoke SetForegroundWindow Sample Code SetForegroundWindow 메쏘드를 이용한 샘플 코드이다. 1초에 한번씩 노트패드에 "abc " 스트링을 입력을 하는 것이다. using System; using System.Diagnostics; using System.Windows.Forms; using System.Runtime.InteropServices; namespace ControlHandle { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { this.timer1.Interval = 1000; this.timer1.Enabled = true.. 이전 1 2 3 4 5 6 7 ··· 9 다음