using System.Collections.Generic; public class ShoppingCart { public ListProducts { 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; } }
decimal cartTotal = cart.TotalPrices();
// DLL을 참조하는 경우에도 해당 클래스에 메소드를 추가한 것처럼 호출할 수 있다.
// DLL을 참조하는 경우에도 해당 클래스에 메소드를 추가한 것처럼 호출할 수 있다.
현재 클래스와 동일한 네임스페이스에 포함되어 있거나, using문으로 지정된 네임스페이스에 존재해야만 한다.