Hi
I am trying a simple Extension Method example and am unable to increment or decrement an int. Here is the code
static class ExtensionMethodsExp
{
public static void Print(this object o)
{
Console.WriteLine("This is print: {0}", o.ToString());
}
public static int Double(this int i)
{
return i * 2;
}
public static int Decrement(this int i)
{
return i--;
}
public static int Increment(this int i)
{
return i++;
}
}
}
Program Code is
class Program
{
static void Main(string[] args)
{
int myNumber = 10;
myNumber.Print();
myNumber = myNumber.Double();
myNumber.Print();
myNumber = myNumber.Decrement();
myNumber.Print();
myNumber = myNumber.Increment();
myNumber.Print();
myNumber.Increment().Double().Print();
Console.ReadKey();
}
}
I am getting following as output
This is print: 10
This is print: 20
This is print: 20
This is print: 20
This is print: 40
Any idea why Decrement or Increment should not work here. Is there something I am missing? thanks