You are talking about the difference between passing by reference and passing by value, which is conceptually similar to the idea of value types vs reference types.
If you pass a value type into the method, you have to use the second example; otherwise you are just incrementing an integer that exists inside the scope of DoSomething(). Try it: if you execute your first example, after DoSomething() has run, the value of your int will be unchanged.
However, if you are passing in something other than a value type (say object foo), you are actually passing a reference to the original object. Anything you do to it inside DoSomething() will take effect outside the method as well, since you are still referring to the same object.
You can accomplish what you're attempting in the first example by writing:
void DoSomething(ref int value)
That instructs .NET to pass a reference to the item regardless of whether it is a value type.
See this writeup on Value Types vs Reference Types on MSDN for a more detailed look.
Additionally, as zodoz points out (upvote appropriately), by returning value++
you are returning and then incrementing. To return the incremented value, use ++value
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…