In C# can we do the following like C++?
public void myMethod(int i, MyClass obj, int value=100){
}
Another question is MyClass is a reference type, if there is no ref before it, it will pass a copy of the MyClass into the method but not the reference?
thanks,
Others have correctly answered the optional parameter part: you can specify a default value for a parameter in C# 4. (There are various restrictions, e.g. mandatory parameters have to come before optional ones, and the default value has to be a compile-time constant.)
<gratuitous plug>See C# in Depth, 2nd edition, chapter 13 for more details</gratuitous plug>For the “parameter passing” aspect – all arguments are passed by value by default, but in the case of reference type the argument is a reference, not the object. Changes to the object will be visible to the caller, but if you change the parameter itself to refer to a different object, that change won’t be visible to the caller. (It won’t change the value of the caller’s variable.) See my article on parameter passing for details.