.NET Tip: Using Extension Methods Wisconsin

Extension methods allow you to create utility functions and have them appear to be part or your classes or the built-in classes. You'll learn by making a method from an earlier tip available as an extension method.

Local Companies

Wireless Direct
1.866.707.8498
BOX 71101
shorewood, WI
R.E. Coker and Associates, Inc.
262-723-8104
108 W Court St.
Elkhorn, WI
R S InfoCon, Inc.
262-898-7456
2320 Renaissance Blvd
Sturtevant, WI
HarrisData
262-784-9099
13555 Bishop's Court, Suite 300
Brookfield, WI
Acumium
608 310 9700 x 522
5133 West Terrace Drive Suite 300
Madison, , WI
Xorbix Technologies Inc.
414-277-5044
759 N. Milwaukee St.
Milwaukee, WI
Visionary Computer Solutions
262-365-9430
PO Box 406
Grafton, WI
IFS
414-577-5191
12000 W. Park Place
Milwaukee, WI
Jela Company Inc
(920) 992-3900
N4977 County Road Ss
Rio, WI
Logicare
(715) 839-0700
2125 Heights Dr Ste 4
Eau Claire, WI


.NET Tip: Using Extension Methods

provided by: 
Originally published at Internet.com


Extension methods allow you to add functionality to existing classes by modifying the original class or using partial classes. In fact, you don't need access to the source of the class at all. You can extend third-party classes and built-in classes as easily as you can extend your own classes. To demonstrate this, I will take the code from a previous tip that uses reflection to display information about an object passed to it and convert it to be an extension method instead. Here is the code for the original DisplayObjectInfo method: public static string DisplayObjectInfo(Object o) { StringBuilder sb = new StringBuilder(); // Include the type of the object System.Type type = o.GetType(); sb.Append("Type: " + type.Name); // Include information for each Field sb.Append("\r\n\r\nFields:"); System.Reflection.FieldInfo[] fi = type.GetFields(); if (fi.Length > 0) { foreach (FieldInfo f in fi) { sb.Append("\r\n " + f.ToString() + " = " + f.GetValue(o)); } } else sb.Append("\r\n None"); // Include information for each Property sb.Append("\r\n\r\nProperties:"); System.Reflection.PropertyInfo[] pi = type.GetProperties(); if (pi.Length > 0) { foreach (PropertyInfo p in pi) { sb.Append("\r\n " + p.ToString() + " = " + p.GetValue(o, null)); } } else sb.Append("\r\n None"); return sb.ToString(); }

This is how you call the DisplayObjectionInfo method: GPSLocation Location = new GPSLocation(39, -86, 50, 180); Debug.Print(Util.DisplayObjectInfo(Location));

And here is the output: Type: GPSLocation Fields: None Properties: Double Latitude = 39 Double Longitude = -86 Int32 Speed = 50 Int32 Direction = 180

There are only a couple things that you need to do to make this into an extension method. First, all extension methods must be defined in a non-generic static class. Second, your method needs to accept a parameter of the type you want to extend. In this case, I want to extend the object data type, so I simply have to prepend the this keyword before the data type of the parameter to the DisplayObjectInfo function. So, my new definition of DisplayObjectInfo as an extension method looks like this: public static class Util2008 { public static string DisplayObjectInfo(this Object o) { ... } }

The body of the method does not change at all. Take a look at how you use the new extension method. GPSLocation Location = new GPSLocation(39, -86, 50, 180); Debug.Print(Location.DisplayObjectInfo());

Now, instead of passing the Location object to the DisplayObjectInfo method, the method has become a part of the Location class. Extension methods are fully integrated into the development environment. This means that your extension methods will show up with Intellisense. Take time to explore extension methods further to see how you can take best advantage of them in your applications.

About the Author

Jay Miller is a Software Engineer with Electronic Tracking Systems, a company dedicated to robbery prevention, apprehension, and recovery based in Carrollton, Texas. Jay has been working with .NET since the release of the first beta and is co-author of Learn Microsoft Visual Basic.Net In a Weekend. Jay can be reached via email at jmiller@sm-ets.com.

Author: Jay Miller

Read article at Internet.com site

Featured Local Company

IFS

414-577-5191
12000 W. Park Place
Milwaukee, WI
www.IFSWORLD.com

IFS provides solutions that enable organizations to become more agile, achieve better business performance, and maintain their competitive advantage. The company pioneered component-based ERP software with IFS Applications, now in its seventh generation. This component architecture provides solutions that are easier to implement, run, and upgrade. IFS targets seven key industries: aerospace & defense, automotive, high-tech, industrial manufacturing, process industries, construction & facilities management, and utilities & telecom.

IFS - Industries
IFS - Solutions

Regional Articles
- .NET Tip: Using Extension Methods Appleton WI
- .NET Tip: Using Extension Methods Baraboo WI
- .NET Tip: Using Extension Methods Beaver Dam WI
- .NET Tip: Using Extension Methods Beloit WI
- .NET Tip: Using Extension Methods Brookfield WI
- .NET Tip: Using Extension Methods Burlington WI
- .NET Tip: Using Extension Methods Cedarburg WI
- .NET Tip: Using Extension Methods Chippewa Falls WI
- .NET Tip: Using Extension Methods Cudahy WI
- .NET Tip: Using Extension Methods De Pere WI
- .NET Tip: Using Extension Methods Delavan WI
- .NET Tip: Using Extension Methods Eau Claire WI
- .NET Tip: Using Extension Methods Elkhorn WI
- .NET Tip: Using Extension Methods Fond Du Lac WI
- .NET Tip: Using Extension Methods Fort Atkinson WI
- .NET Tip: Using Extension Methods Franklin WI
- .NET Tip: Using Extension Methods Green Bay WI
- .NET Tip: Using Extension Methods Hartland WI
- .NET Tip: Using Extension Methods Janesville WI
- .NET Tip: Using Extension Methods Kaukauna WI
- .NET Tip: Using Extension Methods Kenosha WI
- .NET Tip: Using Extension Methods La Crosse WI
- .NET Tip: Using Extension Methods Lake Geneva WI
- .NET Tip: Using Extension Methods Manitowoc WI
- .NET Tip: Using Extension Methods Marinette WI
- .NET Tip: Using Extension Methods Marshfield WI
- .NET Tip: Using Extension Methods Menasha WI
- .NET Tip: Using Extension Methods Menomonee Falls WI
- .NET Tip: Using Extension Methods Menomonie WI
- .NET Tip: Using Extension Methods Merrill WI
- .NET Tip: Using Extension Methods Middleton WI
- .NET Tip: Using Extension Methods Milwaukee WI
- .NET Tip: Using Extension Methods Mosinee WI
- .NET Tip: Using Extension Methods Mukwonago WI
- .NET Tip: Using Extension Methods Muskego WI
- .NET Tip: Using Extension Methods Neenah WI
- .NET Tip: Using Extension Methods New Berlin WI
- .NET Tip: Using Extension Methods Oak Creek WI
- .NET Tip: Using Extension Methods Oconomowoc WI
- .NET Tip: Using Extension Methods Onalaska WI
- .NET Tip: Using Extension Methods Oshkosh WI
- .NET Tip: Using Extension Methods Pewaukee WI
- .NET Tip: Using Extension Methods Racine WI
- .NET Tip: Using Extension Methods Rhinelander WI
- .NET Tip: Using Extension Methods Rice Lake WI
- .NET Tip: Using Extension Methods River Falls WI
- .NET Tip: Using Extension Methods Schofield WI
- .NET Tip: Using Extension Methods Shawano WI
- .NET Tip: Using Extension Methods Sheboygan WI
- .NET Tip: Using Extension Methods South Milwaukee WI
- .NET Tip: Using Extension Methods Stevens Point WI
- .NET Tip: Using Extension Methods Sturgeon Bay WI
- .NET Tip: Using Extension Methods Sun Prairie WI
- .NET Tip: Using Extension Methods Superior WI
- .NET Tip: Using Extension Methods Thiensville WI
- .NET Tip: Using Extension Methods Two Rivers WI
- .NET Tip: Using Extension Methods Watertown WI
- .NET Tip: Using Extension Methods Waukesha WI
- .NET Tip: Using Extension Methods Waupaca WI
- .NET Tip: Using Extension Methods Wausau WI
- .NET Tip: Using Extension Methods West Bend WI
- .NET Tip: Using Extension Methods Whitewater WI
- .NET Tip: Using Extension Methods Wisconsin Rapids WI
Related Local Events
2008 Early Stage Symposium
Dates: 11/5/2008 - 11/6/2008
Location: Monona Terrace
Madison WI
View Details

Wisconsin Entrepreneurs' Conference
Dates: 6/9/2008 - 6/10/2008
Location: Hyatt Regency Hotel
Milwaukee WI
View Details
Rate Article
     
Articles Insider

Rss   Delicious   Digg   Add To My Yahoo   Add To My Google   Bookmark   Search Plugin

Topics:
Advertising Engineering Industrial Goods & Services Software
Business Services Family Insurance Technology
Career Financial Services Internet Telecommunications
Cars Food & Beverage Legal Transportation & Logistics
Computer Hardware Health Real Estate Travel
Construction Home Services Retail & Consumer Services Wedding
Education