Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Saturday, March 19, 2011

Extension Methods in C#

What is an Extension Method ?Extension method helps you to add new methods to an existing types like int,
string or to your custom types(classes).I will explain with an example to make it more clear;Int datatype has a method called ToString().Now I want a method on Int to
find out whether it is an Odd or Even (Say IsEven()). How to do it ? Use Extension Method!

Why Extension Methods ?My first question here is how do we achieve the above task(adding IsEven()
function) without Extension Method?Yes, we can do this by Extending Int type to another class and write IsEven
function there. But in this case you have to create an object of this class
to use this new function. If you use extension methods you don't need to do
all these things!
Suppose if you have a class but no source code and want to add a new function
to this class, use Extension Methods..

How to use Extension Methods ?I will give 2 examples for this. One is adding IsEven() to the built in Int
type. In second example I will show you how to add a new method to our custom class using Extension method.
All your extension methods should be static and it should be in a static
class. Also, the first parameter of the extension method describes to which
type this method should be part of.
Adding IsEven() method to Int Type:
namespace OperationsMethods
{
public static class ExtensionMethods
{
public static bool IsEven(this int val)
{
int result= val%2;
return result == 0 ? true : false;
}
}
}
How to use it? see below code..
protected void Page_Load(object sender, EventArgs e)
{
int num = 100;
bool isEven=num.IsEven();
Response.Write(isEven.ToString());
}


Example 2: Adding method to custom type
Below is my operation class:
namespace Business{
public class operations
{
public operations()
{ }
public int Add(int num1, int num2)
{ return num1 + num2; }
}
}
Now I want to add a new method called Substract() to this class using
extension method. So here goes our Extension method:
namespace OperationsMethods
{
public static class ExtensionMethods
{
public static int Substract(this operations op, int num1, int num2)
{ return num1 - num2; }
}
}
Using this...
protected void Page_Load(object sender, EventArgs e)
{
operations obj = new operations();
int res= obj.Substract(500, 200);
Response.Write(res.ToString());
}

Thursday, October 23, 2008

DateTime formats in .Net

Add to Google

DateTime formats available in .Net

dd - Day number

MM - Month number

yyyy - Year

hh - Hour

mm - Minute

ss - Second

tt - AM/PM

dddd - Day name

MMMM - Month name

How do we use it?

Suppose you want to display today's date in dddd, MMMM dd, yyyy hh:mm tt format;
Response.Write(DateTime.Now.ToString("dddd, MMMM dd, yyyy hh:mm tt"));
The output will be as follows:
Thursday, December 11, 2008 05:30 PM

There are 133 different types of DateTime formats available in .net. Execute the code below to list all the formats for current datetime


string[] DateTimeFormats = DateTime.Now.GetDateTimeFormats();
GridView1.DataSource = DateTimeFormats;
DataBind();


10/9/2009
10/9/09
10/09/09
10/09/2009
09/10/09
2009-10-09
09-Oct-09
Friday, October 09, 2009
October 09, 2009
Friday, 09 October, 2009
09 October, 2009
Friday, October 09, 2009 7:54 PM
Friday, October 09, 2009 07:54 PM
Friday, October 09, 2009 19:54
Friday, October 09, 2009 19:54
October 09, 2009 7:54 PM
October 09, 2009 07:54 PM
October 09, 2009 19:54
October 09, 2009 19:54
Friday, 09 October, 2009 7:54 PM
Friday, 09 October, 2009 07:54 PM
Friday, 09 October, 2009 19:54
Friday, 09 October, 2009 19:54
09 October, 2009 7:54 PM
09 October, 2009 07:54 PM
09 October, 2009 19:54
09 October, 2009 19:54
Friday, October 09, 2009 19:54:25
Friday, October 09, 2009 7:54:25 PM
Friday, October 09, 2009 07:54:25 PM
Friday, October 09, 2009 19:54:25
October 09, 2009 19:54:25
October 09, 2009 7:54:25 PM
October 09, 2009 07:54:25 PM
October 09, 2009 19:54:25
Friday, 09 October, 2009 19:54:25
Friday, 09 October, 2009 7:54:25 PM
Friday, 09 October, 2009 07:54:25 PM
Friday, 09 October, 2009 19:54:25
09 October, 2009 19:54:25
09 October, 2009 7:54:25 PM
09 October, 2009 07:54:25 PM
09 October, 2009 19:54:25
10/9/2009 7:54 PM
10/9/2009 07:54 PM
10/9/2009 19:54
10/9/2009 19:54
10/9/09 7:54 PM
10/9/09 07:54 PM
10/9/09 19:54
10/9/09 19:54
10/09/09 7:54 PM
10/09/09 07:54 PM
10/09/09 19:54
10/09/09 19:54
10/09/2009 7:54 PM
10/09/2009 07:54 PM
10/09/2009 19:54
10/09/2009 19:54
09/10/09 7:54 PM
09/10/09 07:54 PM
09/10/09 19:54
09/10/09 19:54
2009-10-09 7:54 PM
2009-10-09 07:54 PM
2009-10-09 19:54
2009-10-09 19:54
09-Oct-09 7:54 PM
09-Oct-09 07:54 PM
09-Oct-09 19:54
09-Oct-09 19:54
10/9/2009 19:54:25
10/9/2009 7:54:25 PM
10/9/2009 07:54:25 PM
10/9/2009 19:54:25
10/9/09 19:54:25
10/9/09 7:54:25 PM
10/9/09 07:54:25 PM
10/9/09 19:54:25
10/09/09 19:54:25
10/09/09 7:54:25 PM
10/09/09 07:54:25 PM
10/09/09 19:54:25
10/09/2009 19:54:25
10/09/2009 7:54:25 PM
10/09/2009 07:54:25 PM
10/09/2009 19:54:25
09/10/09 19:54:25
09/10/09 7:54:25 PM
09/10/09 07:54:25 PM
09/10/09 19:54:25
2009-10-09 19:54:25
2009-10-09 7:54:25 PM
2009-10-09 07:54:25 PM
2009-10-09 19:54:25
09-Oct-09 19:54:25
09-Oct-09 7:54:25 PM
09-Oct-09 07:54:25 PM
09-Oct-09 19:54:25
October 09
October 09
2009-10-09T19:54:25.2187500+05:30
2009-10-09T19:54:25.2187500+05:30
Fri, 09 Oct 2009 19:54:25 GMT
Fri, 09 Oct 2009 19:54:25 GMT
2009-10-09T19:54:25
7:54 PM
07:54 PM
19:54
19:54
19:54:25
7:54:25 PM
07:54:25 PM
19:54:25
2009-10-09 19:54:25Z
Friday, October 09, 2009 14:24:25
Friday, October 09, 2009 2:24:25 PM
Friday, October 09, 2009 02:24:25 PM
Friday, October 09, 2009 14:24:25
October 09, 2009 14:24:25
October 09, 2009 2:24:25 PM
October 09, 2009 02:24:25 PM
October 09, 2009 14:24:25
Friday, 09 October, 2009 14:24:25
Friday, 09 October, 2009 2:24:25 PM
Friday, 09 October, 2009 02:24:25 PM
Friday, 09 October, 2009 14:24:25
09 October, 2009 14:24:25
09 October, 2009 2:24:25 PM
09 October, 2009 02:24:25 PM
09 October, 2009 14:24:25
October, 2009
October, 2009

Thank you