简体   繁体   中英

Extension method c#

class Program
    {
        public static int i = 0;
        public static void Main()
        {
            (i++).Print();
        }
    }

    static class Extensions
    {
        public static void Print(this int i)
        {
            Console.WriteLine(Program.i);
            Console.WriteLine(i);
        }
    }

OUTPUT: 1 0

Why when we pass just i its not incremented, and with Program.i is incremented?

Firstly, note that this is nothing to do with being an extension method.

The code in Main() is identical to this:

public static void Main()
{
    Extensions.Print(Program.i++);
}

This also prints 1 and 0.

The answer to why this happens is now clearer:

  1. The current value of i , 0, is captured ready to pass it to Print() .
  2. The value of Program.i is incremented, so now it has the value 1.
  3. Print() is called with the unincremented value of i .
  4. Program.i is printed, thus printing 1.
  5. The parameter i is printed, thus printing 0.

In fact, the C# compiler will compile Main() as follows:

public static void Main()
{
    int num = Program.i;
    Program.i = num + 1;
    num.Print();
}

Or (in IL):

.method public hidebysig static void Main () cil managed 
{
    .entrypoint
    IL_0000: nop
    IL_0001: ldsfld int32 Demo.Program::i
    IL_0006: dup
    IL_0007: ldc.i4.1
    IL_0008: add
    IL_0009: stsfld int32 Demo.Program::i
    IL_000e: call void Demo.Extensions::Print(int32)
    IL_0013: nop
    IL_0014: ret
}

The reason the compiler does this is because of the definition of the post-increment operator.

Extensions.Print(Program.i++) by definition must be called with the value of Program.i as it was before it was incremented.

Actually your i increments after the Print() method call ( postfix increment operation , see ++ Operator ), this is why your output is '0', you need to increment it before calling Print() ( prefix increment operation ):

(++i).Print();

with Console.WriteLine(i); .

You can check that, if you call it within a loop, for example:

public static void Main()
{
    for (int a = 0; a < 10; a++)
    {
        (i++).Print();
    }
}

static class Extensions
{
    public static void Print(this string i)
    {
        Console.WriteLine(i);
    }
}

you will get output:

Output: 0 1 2 3 4 5 6 7 8 9

but if you increment i before Print() method call, the result will be:

Output: 1 2 3 4 5 6 7 8 9 10

And your Program.i gets 1, cause its call happens within Print() method and in that point Program 's i will be incremented.

Your output is 0 because it executes the method before incrementing program's variable.

This is how to have 1 as output ^^

class Program
{
    public static int i = 0;
    static void Main(string[] args)
    {
        i++;
        i.Print();
    }
}

static class Extensions
{
    public static void Print(this int i)
    {
        Console.WriteLine(Program.i);
        Console.WriteLine(i);
    }
}

it is because you are using the post-fix increment which means the value is incremented after the print method is executed

class Program
{
    public static int i = 0;
    public static void Main()
    {
        (i++).Print();//  increment after print
        (++i).Print();// print after increment
    }
}

static class Extensions
{
    public static void Print(this int i)
    {
        Console.WriteLine(i);
    }
}

out put

0

2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM