Tweet
SIGN UP
MEMBER LOGIN:
TECHNOLOGIES
.NET 4.5
.NET Remoting in C#
Active Directory C#
ADO.NET in C#
AJAX in C#
Algorithms in C#
Android Programming
Articles C#
ASP, JavaScript, CSS
ASP.NET Controls in C#
ASP.NET MVC with C#
ASP.NET Programming
BizTalk Server
C# Assemblies
C# Language
C# Tutorials
C, C++, MFC
Career Advice
Chapters
Cloud Computing
COBOL.NET
Coding Best Practices
COM Interop
Compact Framework
Cryptography C#
Crystal Reports C#
Current Affairs
Custom Controls C#
Databases & DBA
Deployment
Design & Architecture
DirectX C#
Enterprise Development
Error Zone
Exception Handling C#
Expression Studio
F#
Files, Directories in C#
Financial Applications
Games Programming C#
GDI+ & Graphics
Hardware
How do I
HTML 5
Internet & Web
iPhone/iPad
Java
Java and .NET
JQuery
JSP
Leadership
Learn .NET
LINQ with C#
Metro Style Apps in C#
Mobile & Embedded
MonoDevelop
MSMQ in C#
Multithreading in C#
Networking
Office Development
OOP/OOD
Operating Systems
PHP
Printing in C#
Products
Project Management
Reports using C#
Robotics & Hardware
Security in .NET
SharePoint
Silverlight with C#
Smart Devices
Speech in C#
SQL
SQL Server 2012
String in C#
Team Foundation & VSS
Testing
Visual Basic .NET
Visual C#
Visual Studio .NET
Visual Studio 11
Visual Studio 2010
VS LightSwitch 2011
WCF with C#
Web Forms C#
Web Services in C#
WebForms Controls
Windows 8 in C#
Windows Controls C#
Windows Forms C#
Windows Phone in C#
Windows PowerShell
Windows Services in C#
Workflow Foundation in C#
WPF with C#
XAML with C#
XML in C#
XNA with C#
FORUMS
BLOGS
VIDEOS
INTERVIEWS
CERTIFICATIONS
DOWNLOADS
BOOKS
LINKS
NEWS
Learn .NET in 60 days – Part 1 (13 Labs)
Learn MVC (Model view controller) Step by Step ...
Learn C# Corner - Home
Using Border Radius and Gradients in CSS3: Part I
Learn C# Corner - Footer
Learn .NET and C# in 60 Days Lab13(Day 5): - C ...
iPhone 5 First Look
Samsung Galaxy Note Review
WCF - Authentication and Authorization in Ente ...
How to write a good article on C# Corner
Blog
Usage of Extensions method in C#
Posted by
Aravind BS
in
Blogs
|
C# Language
on
Dec 12, 2011
C# 3.0 has provided extension method. let's know first what is extension method in c#.
Tweet
1695
0
0
As name it self defines, extension method is a method which will extend/add the methods to existing types.Extension methods are special kind of static methods within static class.
Let us consider simple class.
///
This class contains single method Display.
public
class
Class1
{
public
void
Display()
{
MessageBox.Show(
"Hello Aravind"
);
}
}
//Let's consider another class(extension class)
//This is the static class in which static extension method defined.
//Here i have used this key word to extend/Add method to class class1.
public
static
class
ExtendMyMethod
{
public
static
void
Extend(
this
Class1 ObjClass)
{
MessageBox.Show(
"Hello Aravind,You have extended previous method."
);
}
}
//Accessing/using extension method of class1.
public
class
UsageOfExtensionMethod
{
Class1 objCls1=
new
Class1();
//object of class1.
objCls1.Display();
//Normal method defined in class1.
objCls1.Extend();
//Extension method.(Extension).
}
An extension method must be defined in a top-level static class.
An extension method with the same name and signature as an instance method will not be called.
Extension methods cannot be used to override existing methods.
The concept of extension methods cannot be applied to fields, properties or events.
Overuse of extension methods is not good style of programming.
Thanks....
share this blog :
Calculate total price on Textbox..
Leverage “using” in C#
Related Blogs
Usage of Extensions method in C# -Example
Practical usage of Find
Method in Generic List
using Predicate
Anonymous Methods in C#
Anonymous Methods in C# without parameters
Extension Methods in C-Sharp
Extension methods in C# 3.0
post comment
Sponsored by
Become a Sponsor
More Blogs from this Blogger
Usage of Extensions method in C#
Usage of Extensions method in C# -Example
View All
Latest Blogs
WebPart Collector or WebPart Finder
Free Ride is Over for Desktop Developers in Visual Studio 11
DotNet developers most used application/tools launching through 'Run'
80-inch Windows 8 Tablet
Data encapsulation
Option to access the Column Names in Data table
Open multiple windows in browser startup. (Multiple homepages option)
I'm Sorry
const and readonly
Address, Binding and Contract in WCF
View All
Sponsored by
Become a Sponsor