public static void DelegateExample()
        {
            ReferenceTypes rt = new ReferenceTypes();

            TestDelegateA delegateA = rt.Hello;   // assign it value
            TestDelegateB delagateB = rt.Adder;

            delegateA("world");                   // invoke it
            long result = delagateB(100, 200);

            Console.WriteLine("Result = {0}", result);
        }
Ejemplo n.º 2
0
 public static void Main()
 {
     TestDelegateA a = new TestDelegateA(Delegate);
     TestDelegateB b = new TestDelegateB(a);
 }
Ejemplo n.º 3
0
        public static void Main(string[] args)
        {
            //throw new Exception(); // 즉시 멈춘다

            /*
             * try
             * {
             *      throw new Exception();
             * }
             * catch (Exception ex)
             * {
             *      Console.WriteLine("예외가 발생했습니다.");
             * }
             */

            Box bb = new Box(10, 20);

            Console.WriteLine(bb.Area());

            try
            {
                throw new CustomException("사용자 정의 예외");
            }
            catch (CustomException ex)
            {
                Console.WriteLine(ex.Message);
            }

            List <Product> prod = new List <Product>()
            {
                new Product()
                {
                    Name = "Potato", Price = 500
                },
                new Product()
                {
                    Name = "Tomato", Price = 700
                },
                new Product()
                {
                    Name = "Banana", Price = 400
                },
                new Product()
                {
                    Name = "Apple", Price = 600
                },
                new Product()
                {
                    Name = "Cherry", Price = 500
                }
            };

            // 정렬
            //prod.Sort(); // IComparision 인터페이스가 필요함
            //prod.Sort(SortWithPrice); // SortWithPrice 델리게이트 이용
            prod.Sort(delegate(Product a, Product b)             // 무명 델리게이터를 이용
            {
                return(a.Price.CompareTo(b.Price));
            });
            prod.Sort((a, b) =>             // 람다함수
            {
                return(a.Name.CompareTo(b.Name));
            });
            prod.Sort((a, b) => a.Price.CompareTo(b.Price));


            // 출력
            foreach (var item in prod)
            {
                Console.WriteLine(item.Name + " : " + item.Price);
            }

            TestDelegateA deleA = TestDeleMethod;        // 초기화시 () 사용안함
            TestDelegateA deleB = delegate() { };        // 무명 델리게이터
            TestDelegateA deleC = () => { };             // 람다

            deleA();
            deleB();
            deleC();

            Students student1 = new Students();

            student1.Add(new Student("홍길동", 4.2));
            student1.Add(new Student("사임당", 4.2));

            student1.Print();

            /*
             * student1.Print((std) => {
             *      Console.WriteLine();
             *      Console.WriteLine("이름 : " + std.Name);
             *      Console.WriteLine("점수 : " + std.Score);
             * });
             */

            student1.Print(delegate(Student std){              // 무명 델리게이트
                Console.WriteLine();
                Console.WriteLine("이름 : " + std.Name);
                Console.WriteLine("점수 : " + std.Score);
            });

            student1.Print(PrintWithParameter);
        }
Ejemplo n.º 4
0
	public static void Main() 
	{
		TestDelegateA a = new TestDelegateA (Delegate);
		TestDelegateB b = new TestDelegateB (a);
	}