TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Extension Method in C#
Rajan Singh
Sep 19
2015
Code
844
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
using
System;
class
MyData {
private
double
D1;
// Fields
private
double
D2;
private
double
D3;
public
MyData(
double
d1,
double
d2,
double
d3)
// Constructor
{
D1 = d1;
D2 = d2;
D3 = d3;
}
public
double
Sum()
// Method Sum
{
return
D1 + D2 + D3;
}
}
static
class
ExtendMyData {
public
static
double
Average(MyData md) {
return
md.Sum() / 3;
}
}
class
UsingExtendMyData {
public
static
void
Main() {
MyData md =
new
MyData(3, 4, 5);
Console.WriteLine(
"Average: {0}"
, ExtendMyData.Average(md));
}
}
Output:
Average: 4
Extension Method
C#