Find Code:
All Words
Any of the Words
Exact Phrase
Home
:
Code
:
Forums
:
Submit
:
Mailing List
:
About
:
Contact
Code
All
VB.NET
ASP.NET
C#
VB Classic
ASP Classic
Snippets
Popular
Resources
Submit Code
Forums
Articles
Tips
Links
Books
Contest
Link to us
Extension Method
Author:
Arindam Ghosh
E-mail:
Click to e-mail author
Website:
http://program2program.blogspot.in
Submitted:
1/10/2014
Version:
Compatibility:
C# 2010, C# 2012
Category:
ASP
Downloads:
1970
There is no whole in my piggy bank but the money is still being inserted] Extension Method is that kind of feature. Suppose we have a base class Employee. This class should not be modified but I need to insert some methods in the Employee class. No need for deriving the Employee class. Now what is the solution? The solution is Extension Method. Extension methods enables you to add methods to existing types without creating new derived type, recompiling or modifying the original type. Extension Methods are a special kind of static method. Example:- I will add the following method to String class in my program without derivation or modifying the String Class. public static String GetFirstThreeCharacters(this String str) { if (str.Length < 3) { return str; } else { return str.Substring(0, 3); } } Just I have to add a static class like this . public static class Extension { public static String GetFirstThreeCharacters(this String str) { if (str.Length < 3) { return str; } else { return str.Substring(0, 3); } } } Then I can use the GetFirstThreeCharacters in this manner in my Form Load Event. private void Form1_Load(object sender, EventArgs e) { String st = "My Name is Arindam"; MessageBox.Show(st.GetFirstThreeCharacters()); }
Home
|
Forums
|
Submit
|
Books
|
Mailing List
|
Advertising
|
About
|
Contact
© 2024 A1VBCode. All rights reserved.
Legal disclaimer & terms of use
Privacy statement