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을 참조하는 경우에도 해당 클래스에 메소드를 추가한 것처럼 호출할 수 있다.
// DLL을 참조하는 경우에도 해당 클래스에 메소드를 추가한 것처럼 호출할 수 있다.
현재 클래스와 동일한 네임스페이스에 포함되어 있거나, using문으로 지정된 네임스페이스에 존재해야만 한다.