본문 바로가기

Code

(117)
[C#] Thread 샘플 코드 3 Thread에 관한 3번째 샘플 코드이다. 이 샘플 코드는 Thread를 3개 실행 시키는 것이다. using System; using System.Threading; public class NameUsingThread { private int time; private Thread thread; public NameUsingThread(String n, int t) { time = t; thread = new Thread(new ThreadStart(Run)); thread.Name = n; thread.Start(); } public void Run() { for (int i = 1; i
[C#] Thread 샘플 코드 2 이것은 앞의 예제를 약간 변형한 것이다. 2개의 Thread가 하나의 공통된 변수를 다루고 있다. 2개의 Thread가 하나의 int 변수에 발생한 난수를 더하고 있다. using System; using System.Threading; namespace SampleThread { class Program { static void Main(string[] args) { myRandom = new Random(); s = 0; Thread t1 = new Thread(new ThreadStart(method1)); t1.Start(); Thread t2 = new Thread(new ThreadStart(method2)); t2.Start(); } private static void method1() { ..
[C#] Thread 샘플 코드 1 아래는 간단한 Thread 샘플 코드이다. 난수를 발생시키는 2개의 Thread가 있고, 발생한 난수만큼 Sleep 한다. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace SampleThread { class Program { static void Main(string[] args) { myRandom = new Random(); Thread t1 = new Thread(new ThreadStart(method1)); t1.Start(); Thread t2 = new Thread(new ThreadStart(method2)); t2.Star..
[C#] Extension Methods 샘플 코드 아래는 Extension Methods 예제이다. LINE A는 Extension Methods를 사용했고, LINE B는 Static Method를 사용했다. 둘의 선언 방법과 사용 방법을 비교해보자. Extension methods are static methods. Extension methods can only be declared in static classes. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string s = "9"; int ..
[C#] 중복되지 않는 난수 발생 샘플 코드 난수를 발생할 때, 중복되지 않는 것들로만 해야할 때가 있다. 이와 같은 경우에 사용할 수 있는 예제를 하나 만들어 보았다. List Generic Collection을 사용해서 만들었다. using System; using System.Collections.Generic; namespace RandomNumber { class Program { static void Main(string[] args) { Random myRandom = new Random(); List myList = new List(); int r; for (int i = 0; i < 3; i++) { do { r = myRandom.Next(5); } while (myList.Contains(r)); myList.Add(r); } ..
[C#] Jagged Array 선언 방법 Jagged Array는 Multidimensional Array와는 다르므로 주의를 요합니다. 아래는 Jagged Array 선언 방법을 보여주는 샘플 코드입니다. Multidimensional Array와 헷갈리지 않도록 앞에 있는 Multidimensional Array 선언 방법과 비교해가며 보시기 바랍니다. namespace ArraySample { class Program { static void Main(string[] args) { // Jagged Array int[][] zArray; zArray = new int[3][]; zArray[0] = new int[] { 1, 2, 3 }; zArray[1] = new int[] { 1, 2 }; zArray[2] = new int[] { ..
[C#] 3차원 Array 샘플 코드 아래는 3차원 Array 선언 방법을 보여주는 샘플 코드입니다. 조금 헷갈릴 수 있는데, 잘 살펴보시기 바랍니다. namespace ArraySample { class Program { static void Main(string[] args) { // An example of a three-dimensional array: int[, ,] array3D = new int[2, 3, 2] { { { 1, 2 }, { 1, 2 }, { 1, 2 } }, { { 1, 2 }, { 1, 2 }, { 1, 2 } } }; } } }
[C#] Multidimensional Array 선언하는 방법 Array 선언 방법과 마찬가지로 Multidimensional Array 선언 방법도 몇가지가 있습니다. 아래는 Multidimensional Array 선언 방법을 보여주는 샘플 코드입니다. namespace ArraySample { class Program { static void Main(string[] args) { // Multidimensional Array int[,] array1; array1 = new int[2, 3]; int[,] array2 = new int[2, 3]; int[,] array3 = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } }; int[,] array4 = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } }; in..