static void Main(string[] args) { PrimeGenerator gen = new PrimeGenerator(); //PrintPrime 추가 PrimeGenerator.PrimeDelegate callprint = PrintPrime; gen.AddDelegate(callprint); //SumPrime 추가 PrimeGenerator.PrimeDelegate callsum = SumPrime; gen.AddDelegate(callsum); // if callbacks public. ////PrintPrime 추가 //PrimeGenerator.PrimeDelegate callprint = PrintPrime; //gen.callbacks += callprint; ////SumPrime 추가 //PrimeGenerator.PrimeDelegate callsum = SumPrime; //gen.callbacks += callsum; // 1 ~ 10 까지 소수를 구하고, gen.Run(10); Console.WriteLine(); Console.WriteLine(Sum); // SumPrime 콜백 메서드를 제거한 후 다시 1 ~ 15 까지 소스를 구하는 메서드 호출 gen.RemoveDelegate(callsum); gen.Run(15); }
public static void Run() { PrimeGenerator generator = new PrimeGenerator(); PrimeGenerator.PrimeDelegate printDelegate = PrintPrime; generator.AddDelegate(printDelegate); PrimeGenerator.PrimeDelegate sumDelegate = SumPrime; generator.AddDelegate(sumDelegate); generator.Run(10); Console.WriteLine(); Console.WriteLine(mSum); generator.RemoveDelegate(sumDelegate); generator.Run(15); Console.WriteLine(); }
static void Main(string[] args) { PrimeGenerator gen = new PrimeGenerator(); PrimeGenerator.PrimeDelegate callprint = PrintPrime; gen.AddDelegate(callprint); PrimeGenerator.PrimeDelegate callsum = SumPrime; gen.AddDelegate(callsum); gen.Run(10); Console.WriteLine(); Console.WriteLine(Sum); gen.RemoveDelegate(callsum); gen.Run(15); }
public static void Main(string[] args) { PrimeGenerator gen = new PrimeGenerator(); // 콜백 메소드 추가 PrimeGenerator.PrimeDelegate callprint = PrintPrime; gen.AddDelegate(callprint); PrimeGenerator.PrimeDelegate callsum = SumPrime; gen.AddDelegate(callsum); // 2 ~ 10까지의 소수를 판별하면서 콜백 함수 호출 gen.Run(10); Console.WriteLine($"sum: {sum}"); gen.RemoveDelegate(callsum); gen.Run(15); }
static void Main(string[] args) { PrimeGenerator gen = new PrimeGenerator(); //PrintPrime 콜백 메서드 추가 PrimeGenerator.PrimeDelegate callPrint = PrintPrime; gen.AddDelegate(callPrint); //SumPrime 콜백 메서드 추가 PrimeGenerator.PrimeDelegate callsum = SumPrime; gen.AddDelegate(callsum); //1~10까지 소수를 구하기 gen.Run(10); Console.WriteLine(); Console.WriteLine(Sum); //SumProme 콜백 메서드를 제거한 후 다시 1~15까지 소수를 구하는 메서드 호출 gen.RemoveDelegate(callsum); gen.Run(15); }
public static void Main() { PrimeGenerator gen = new PrimeGenerator(); // 소수생성기 인스턴스 생성 // PrintPrime 콜백 메서드 추가 PrimeGenerator.PrimeDelegate callprint = PrintPrime; // 소수출력 메소드를 델리게이트로 생성 gen.AddDelegate(callprint); // 델리게이트로 만든 소수출력 메소드를 소수생성기의 델리게이트와 연결 // SumPrime 콜백 메서드 추가 PrimeGenerator.PrimeDelegate callsum = SumPrime; // 소수합 메소드를 델리게이트로 생성 gen.AddDelegate(callsum); // 델리게이트로 만든 소수합 메소드를 소수생성기의 델리게이트와 연결 // 1 ~ 10까지 소수를 구하고, gen.Run(10); // 델리게이트와 연결된 모든 메소드를 동시에 실행 Console.WriteLine(); Console.WriteLine(Sum); // SumPrime 콜백 메서드를 제거한 후 다시 1 ~ 15까지 소수를 구하는 메서드 호출 gen.RemoveDelegate(callsum); // 델리게이트 구독 해지 gen.Run(15); }
/// <summary> /// 프로그램 메인 /// </summary> /// <param name="args"></param> static void Main(string[] args) { PrimeGenerator gen = new PrimeGenerator(); /// 대리자함수로 등록하는 과정. 이벤트가 발생할때, 해당 함수가 실행될 것임. PrimeGenerator.PrimeDelegate callprint = PrintPrime; gen.AddDelegate(callprint); PrimeGenerator.PrimeDelegate callsum = SumPrime; gen.AddDelegate(callsum); // 사용자의 콘솔입력을 받아서 실행됨. 숫자가 아닌 잘못된 값을 받았을때 계속 Loop됨. //Console.WriteLine("범위값을 설정하여 주십시오."); string input = string.Empty; int num; while (int.TryParse(input, out num) == false) { Console.WriteLine("범위값을 설정하여 주십시오."); input = Console.ReadLine(); } Console.WriteLine("0~{0} 사이의 소수들을 구합니다.", num); gen.Run(num); Console.WriteLine(); Console.WriteLine("모든 소스들의 합은 {0} 입니다.", Sum); // SumPrime 대리자 함수등록를 해지합니다. 더이상 이 이벤트발생시, 해당함수가 실행되지 않습니다. PrintPrime 대리자는 아직 남아있어 그대로 작동합니다. gen.RemoveDelegate(callsum); gen.Run(99); Console.WriteLine(); Console.Write("모든 소스들의 합은 "); Console.WriteLine("{0} 입니다.", Sum); // 아무키나 입력하게 되면, 프로그램을 종료합니다. Console.WriteLine(); Console.WriteLine("프로그램을 끝냅니다."); Console.ReadLine(); }
static void Main(string[] args) { #region 245 /* * Days workingDays = Days.Monday | Days.Tuesday | Days.Wednesday | Days.Thursday | Days.Friday; * * Console.WriteLine(workingDays.HasFlag(Days.Sunday)); // Sunday를 포함하고 있는가? * * Days today = Days.Friday; * Console.WriteLine(workingDays.HasFlag(today)); // today를 포함하고 있는가? * * Console.WriteLine(workingDays);*/ #endregion 245 #region 246 /* * Mathematics math = new Mathematics(); * * WorkDelegate work = math.Calculate; * * work(CalcType.Add, 10, 5); * work(CalcType.Minus, 10, 5); * work(CalcType.Multiply, 10, 5); * work(CalcType.Divide, 10, 5);*/ #endregion 246 #region 247 /* * Scheduler sched = new Scheduler(); * sched.Run();*/ #endregion 247 #region 248 /* * Point pt = new Point(5, 10); * Point pt2 = new Point(pt.X + 1, pt.Y + 1);*/ #endregion 248 #region 250 /* * int x = 5; * int y = 10; * * Console.WriteLine("x 변수의 값: " + x); * Console.WriteLine("y 변수의 값: " + y); * * System.Console.WriteLine("x" + TEXT + x); * System.Console.WriteLine("y" + TEXT + y); * * Console.WriteLine("sbyte: " + sbyte.MinValue + " ~ " + sbyte.MaxValue); * Console.WriteLine("byte: " + byte.MinValue + " ~ " + byte.MaxValue); * Console.WriteLine("short: " + short.MinValue + " ~ " + short.MaxValue); * Console.WriteLine("ushort: " + ushort.MinValue + " ~ " + ushort.MaxValue); * Console.WriteLine("char: " + (int)char.MinValue + " ~ " + (int)char.MaxValue); * Console.WriteLine("sbyte: " + sbyte.MinValue + " ~ " + sbyte.MaxValue); * Console.WriteLine("int: " + int.MinValue + " ~ " + int.MaxValue); * Console.WriteLine("uint: " + uint.MinValue + " ~ " + uint.MaxValue); * Console.WriteLine("long: " + long.MinValue + " ~ " + long.MaxValue); * Console.WriteLine("ulong: " + ulong.MinValue + " ~ " + ulong.MaxValue); * Console.WriteLine("float: " + float.MinValue + " ~ " + float.MaxValue); * Console.WriteLine("double: " + double.MinValue + " ~ " + double.MaxValue); * Console.WriteLine("decimal: " + decimal.MinValue + " ~ " + decimal.MaxValue);*/ #endregion 250 PrimeGenerator gen = new PrimeGenerator(); // PrintPrime 콜백 메서드 추가 PrimeGenerator.PrimeDelegate callprint = PrintPrime; gen.AddDelegate(callprint); // SumPrime 콜백 메서드 추가 PrimeGenerator.PrimeDelegate callsum = SumPrime; gen.AddDelegate(callsum); // 1 ~ 10까지 소수를 구하고, gen.Run(10); Console.WriteLine(); Console.WriteLine(Sum); // SumPrime 콜백 메서드를 제거한 후 다시 1 ~ 15까지 소수를 구하는 메서드 호출 gen.RemoveDelegate(callsum); gen.Run(15); }