Articles → .NET → Extension Method in C#

Extension Method in C#












Create a custom class


public class UsefulFunctions
{
  public bool IsEvenNumber(int num)
      	  { 
		  …
  }
}









Create extension method










public static class ExtensionMethodsClass
{
  public static bool IsEvenNumber(this int num)
  {
    		bool status = false;
    	  if (num % 2 == 0)
    		  {
    	      status = true;
    		  }
    	  return status;
  }	
}




int x = 3;
MessageBox.Show(x.IsEvenNumber().ToString());





Posted By  -  Karan Gupta
 
Posted On  -  Wednesday, February 23, 2011

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250