Modify method parameter within method or return result

前端 未结 7 2407
余生分开走
余生分开走 2020-12-05 02:47

What is the difference between

private void DoSomething(int value) {
    value++;
}

and

private int DoSomething(int value)          


        
7条回答
  •  我在风中等你
    2020-12-05 03:20

    Everyone else seems to be suggesting the difference in variable passing, but I noticed something different:

    If the example code you are displaying is a simplification of something you are already looking at, then you might want to note that in your second example:

    private int DoSomething(int value) {
        return value++;
    }
    

    The value will return then increment. So if you do the following:

    public Main() {
        int val = 1;
        Console.writeln(DoSomething(val));
    }
    

    Your output will be 1.

    Let me know if this helps at all.

提交回复
热议问题