'Extension Method'에 해당되는 글 1건

  1. 2013.05.29 확장메소드 사용하기

확장메소드 사용하기

IT/C# 2013. 5. 29. 09:26
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using System.Collections.Generic;
 
public class ShoppingCart {
     public List<product> Products { get; set; }
}
 
public static class MyExtensionMethods {
     public static decimal TotalPrices(this ShoppingCart cartParam) {
          decimal total = 0;
          foreach(Product prod in cartParam.Products) {
               total += prod.Price;
          }
          return total;
     }
}
</product>

decimal cartTotal = cart.TotalPrices();
// DLL을 참조하는 경우에도 해당 클래스에 메소드를 추가한 것처럼 호출할 수 있다.

현재 클래스와 동일한 네임스페이스에 포함되어 있거나, using문으로 지정된 네임스페이스에 존재해야만 한다.
Posted by lI헐헐Il
,