Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › Spot welding with C# (Extension methods)
New Posts  All Forums:Forum Nav:

Spot welding with C# (Extension methods)

post #1 of 5
Thread Starter 
After a few topics on the deep end of the magic that happens inside the computer, this time we will move up the ladder slightly to demonstrate a higher level concept that was introduced with Visual C# 3.0 and Visual Basic .NET 9: Extension methods. We will be focusing on how to use this concept in C# only.

What are extension methods?

Extension methods are a compiler-level "syntactic sugar" that basically allow you to spot weld additional methods onto classes that already exist, and make them appear (via compiler voodoo) as if they were defined in the class to start with. The important thing that I would like to reiterate is that extension methods are implemented at the compiler-level.

What is the point of an extension method?

This is a good question, because at first you might not really know what to use this language feature for. As said before, extension methods allow you to bolt on additional functionality to an existing class and make that functionality accessible in the exact same way as if it were defined in the class to start with. They are mainly used to simplify operations on an object and to permit code re-use. It is also handy when you want to bold on functionality to a class of which you do not control the source code (for example from a 3rd party assembly).

For example, lets take the String class in the .NET Framework (System.String). For the sake of the example, let say that my program converts lots of strings to upper case, and then writes them out to the console. Assuming extension methods did not exist, I could implement this two ways:

1. Call System.String.ToUpper() and Console.WriteLine() each time I need this operation.
2. Create a static helper class (for example StringHelpers) and define a static method method that does the same as (1). Call the static helper method each time I need this operation.

Of course, this section would hardly be complete without a code sample:

Implementation 1:
Code:
string example = "VB is better than Huddler";
Console.WriteLine( example.ToUpper() );

Of course, this implementation does not permit code re-use. What if we decided to convert the string to lowercase in a later revision of the program? We'd need to change many places in the program!

Implementation 2:
Code:
public static class StringHelpers
{
    public static void ConvertAndPrint( string input )
    {
        Console.WriteLine( input.ToUpper() );
    }
}

/* Usage */
string example = "VB is better than Huddler";
StringHelpers.ConvertAndPrint( example );

This implementation is better because it provides the ability to re-use code and makes it easier to change the behavior (only need to modify a single location). However, it is not quite as elegant as an extension method.

Implementing an extension method.

Interestingly enough, an extension method in C# is very similar to our static class + static method described in Implementation 2 above. In C#, an extension method must be a static method inside a static class, however for each method that is to be an extension method, the first parameter is decorated with the "this" modifier - this is the clue to the compiler that it is an extension method. Additionally, the first parameter MUST be the type/class that we want to extend. I'll demonstrate this below, and we will stick with our example from above:
Code:
 /* You can name the class anything, but I like to name it with the class I am extending and suffix it with "Extensions" */
public static class StringExtensions
{
    /* Note the "this" modifier before the first parameter */
    public static void ConvertAndPrint( this string input ) 
    {
        /* We should always check for null in an extension method */
        if ( string.IsNullOrEmpty( input ) )
        {
            return;
        }

        Console.WriteLine( input.ToUpper() );
    }
}

Now, here is the fun part where we get to use the magic of the compiler. We can call the extension method directly off our string instance variable, like this (continuing the example):
Code:
/* Usage */
string example = "VB is better than Huddler";
example.ConvertAndPrint();

The compiler automatically passes the instance to the extension method, so we call the method without any parameters - and it looks just like the ConvertAndPrint method was defined in the System.String class to begin with! Behind the scenes, though, the compiler generates code that does exactly the same as Implementation 2 above. You will notice this is the case if you use a program like Reflector to inspect the generated IL of both examples (advanced users).

I am getting a "Cannot resolve symbol XXX", where "XXX" is the name of my extension method. What is going on?

I did not use namespaces in my examples above, however if you defined your extension method static class inside a namespace and are trying to call it from a different namespace, you must import the namespace (in C# this is done via the using statement) from which the extension method static class is defined.

Other mistakes include not using a static class or method, or forgetting the "this" modifier on the first parameter of the extension method.

Happy welding!
Edited by tompsonn - 9/1/12 at 10:21pm
My System
(30 items)
 
"Zeus"
(13 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i5 2500K (4.5ghz @ 1.320v) Gigabyte Z68X-UD3R-B3 MSI R7970 Lightning Corsair 16GB (4x4GB) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Crucial M4 128GB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 7 Ultimate 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2212HM Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Microsoft IntelliMouse Optical  XTRAC Ripper XXL 
AudioAudioAudioAudio
Westone W3 IEMs RE-272 IEMs Shure SE-215 IEMs Schiit Bifrost DAC 
AudioAudio
Schiit Asgard 2 amp HiVi Swan M50W 2.1 
CPUMotherboardGraphicsRAM
Intel Core i7 950 GA-X58-UD3R Radeon HD 5450  24GB Corsair @ 1333mhz 
Hard DriveOSPowerCase
4x WD Cavair Red 1TB in RAID 0 Windows Server 2008 R2 x64 Corsair HX-520 LianLi LanCool 
  hide details  
Reply
My System
(30 items)
 
"Zeus"
(13 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i5 2500K (4.5ghz @ 1.320v) Gigabyte Z68X-UD3R-B3 MSI R7970 Lightning Corsair 16GB (4x4GB) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Crucial M4 128GB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 7 Ultimate 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2212HM Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Microsoft IntelliMouse Optical  XTRAC Ripper XXL 
AudioAudioAudioAudio
Westone W3 IEMs RE-272 IEMs Shure SE-215 IEMs Schiit Bifrost DAC 
AudioAudio
Schiit Asgard 2 amp HiVi Swan M50W 2.1 
CPUMotherboardGraphicsRAM
Intel Core i7 950 GA-X58-UD3R Radeon HD 5450  24GB Corsair @ 1333mhz 
Hard DriveOSPowerCase
4x WD Cavair Red 1TB in RAID 0 Windows Server 2008 R2 x64 Corsair HX-520 LianLi LanCool 
  hide details  
Reply
post #2 of 5
Subbed, fun read thumb.gif
R.E.D (dead)
(5 items)
 
Wife's D3 machine
(13 items)
 
 
CPUGraphicsRAMHard Drive
Core 2 Duo P8400 Nvidia 9600M GT 2x2Gb Hynx DDR2 WD Scorpio Blue 
OS
Windows 7 x64 Professional 
CPUMotherboardGraphicsRAM
Intel Pentium G840 DualCore SB 2.8Ghz AsRock H61M-VS Inno3D 8800 GTS 320 Kingston HyperX 1600 4Gb 
Hard DriveOptical DriveCoolingOS
WD Scorpio Blue 250Gb LiteOn DVD-RW stock Win 7 Home Premium 
MonitorKeyboardPowerCase
Acer 19" 1440x900 Logitech MX3200 beQuiet 300W MS-Tech CA-0130 Black 
Mouse
Logitech MX300 Laser 
  hide details  
Reply
R.E.D (dead)
(5 items)
 
Wife's D3 machine
(13 items)
 
 
CPUGraphicsRAMHard Drive
Core 2 Duo P8400 Nvidia 9600M GT 2x2Gb Hynx DDR2 WD Scorpio Blue 
OS
Windows 7 x64 Professional 
CPUMotherboardGraphicsRAM
Intel Pentium G840 DualCore SB 2.8Ghz AsRock H61M-VS Inno3D 8800 GTS 320 Kingston HyperX 1600 4Gb 
Hard DriveOptical DriveCoolingOS
WD Scorpio Blue 250Gb LiteOn DVD-RW stock Win 7 Home Premium 
MonitorKeyboardPowerCase
Acer 19" 1440x900 Logitech MX3200 beQuiet 300W MS-Tech CA-0130 Black 
Mouse
Logitech MX300 Laser 
  hide details  
Reply
post #3 of 5
Thread Starter 
Quote:
Originally Posted by ronnin426850 View Post

Subbed, fun read thumb.gif

Thanks! biggrin.gif
My System
(30 items)
 
"Zeus"
(13 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i5 2500K (4.5ghz @ 1.320v) Gigabyte Z68X-UD3R-B3 MSI R7970 Lightning Corsair 16GB (4x4GB) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Crucial M4 128GB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 7 Ultimate 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2212HM Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Microsoft IntelliMouse Optical  XTRAC Ripper XXL 
AudioAudioAudioAudio
Westone W3 IEMs RE-272 IEMs Shure SE-215 IEMs Schiit Bifrost DAC 
AudioAudio
Schiit Asgard 2 amp HiVi Swan M50W 2.1 
CPUMotherboardGraphicsRAM
Intel Core i7 950 GA-X58-UD3R Radeon HD 5450  24GB Corsair @ 1333mhz 
Hard DriveOSPowerCase
4x WD Cavair Red 1TB in RAID 0 Windows Server 2008 R2 x64 Corsair HX-520 LianLi LanCool 
  hide details  
Reply
My System
(30 items)
 
"Zeus"
(13 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i5 2500K (4.5ghz @ 1.320v) Gigabyte Z68X-UD3R-B3 MSI R7970 Lightning Corsair 16GB (4x4GB) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Crucial M4 128GB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 7 Ultimate 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2212HM Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Microsoft IntelliMouse Optical  XTRAC Ripper XXL 
AudioAudioAudioAudio
Westone W3 IEMs RE-272 IEMs Shure SE-215 IEMs Schiit Bifrost DAC 
AudioAudio
Schiit Asgard 2 amp HiVi Swan M50W 2.1 
CPUMotherboardGraphicsRAM
Intel Core i7 950 GA-X58-UD3R Radeon HD 5450  24GB Corsair @ 1333mhz 
Hard DriveOSPowerCase
4x WD Cavair Red 1TB in RAID 0 Windows Server 2008 R2 x64 Corsair HX-520 LianLi LanCool 
  hide details  
Reply
post #4 of 5
Interesting
    
CPUMotherboardGraphicsGraphics
Intel Core i7 860 Asus P7P55D-E Pro MSI GTX560 Ti TwinFrozr II SLI MSI GTX560 Ti TwinFrozr II SLI 
RAMHard DriveHard DriveHard Drive
Corsair 8GB DDR3 1600MHz CL9 XMS3 (2 x 4GB) OCZ Vertex 3 SSD Western Digital Caviar Black 1TB 7200RPM 64MB C... Western Digital Caviar Green 1TB ~5900RPM 64MB ... 
Optical DriveCoolingOSMonitor
Lite-On 24x DVD-RW CoolerMaster V8 Windows 7 Professional SP1 3 x Samsung S22B350H 
KeyboardPowerCaseMouse
Ducky Shine II Corsair HX850 CoolerMaster Storm Enforcer Logitech M500 
Mouse PadAudio
Razer Goliathus Microsoft LifeChat LX 3000 
  hide details  
Reply
    
CPUMotherboardGraphicsGraphics
Intel Core i7 860 Asus P7P55D-E Pro MSI GTX560 Ti TwinFrozr II SLI MSI GTX560 Ti TwinFrozr II SLI 
RAMHard DriveHard DriveHard Drive
Corsair 8GB DDR3 1600MHz CL9 XMS3 (2 x 4GB) OCZ Vertex 3 SSD Western Digital Caviar Black 1TB 7200RPM 64MB C... Western Digital Caviar Green 1TB ~5900RPM 64MB ... 
Optical DriveCoolingOSMonitor
Lite-On 24x DVD-RW CoolerMaster V8 Windows 7 Professional SP1 3 x Samsung S22B350H 
KeyboardPowerCaseMouse
Ducky Shine II Corsair HX850 CoolerMaster Storm Enforcer Logitech M500 
Mouse PadAudio
Razer Goliathus Microsoft LifeChat LX 3000 
  hide details  
Reply
post #5 of 5
WARNING!!!!

Do NOT misuse the mechanic as the default way to add all kinds of stuff to existing "core" classes, those that make up the basic building blocks of your application, like string, int, long, and DateTime classes.

Next thing you'll know you end up with hundreds of methods on your String class that are useful but not quite because they're too specific to sit at that common level.

Pick the right class to extend. What you have to think about is "How useful is the extention from the perspective of using that class. Will I use it often and in what kinds of different scenarios?" If you can't really come up with ideas, your function may be too specific to be added as an extention method.

When you write stuff for re-use, always question its "usability scope". Try to eliminate parameters specific to a certain thing you're trying to implement etc. Books can be written about this =)
The Box
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 D0 4.33GHz HT On (21x206)(1.39v) Foxconn Bloodrage Club 3D 4870x2 (Catalyst 10.2) 6GB OCZ Blade 7-8-7 2GHz (1650MHz, 6-6-6) 
Hard DriveOSMonitorKeyboard
ICH: 2x60GB-R0(SSD),2x160GB-R0 EX4650: 4x1.5TB-R5 Windows 7 Ultimate Samsung 244T Cheap but rugged! 
PowerCaseMouse
SilverStone Strider 850W Mountain Mods U2-UFO Logitech G9 
  hide details  
Reply
The Box
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 D0 4.33GHz HT On (21x206)(1.39v) Foxconn Bloodrage Club 3D 4870x2 (Catalyst 10.2) 6GB OCZ Blade 7-8-7 2GHz (1650MHz, 6-6-6) 
Hard DriveOSMonitorKeyboard
ICH: 2x60GB-R0(SSD),2x160GB-R0 EX4650: 4x1.5TB-R5 Windows 7 Ultimate Samsung 244T Cheap but rugged! 
PowerCaseMouse
SilverStone Strider 850W Mountain Mods U2-UFO Logitech G9 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › Spot welding with C# (Extension methods)