D
David Levine
This is really more of a "why was C# designed this way?" type of question.
Hope this is the write forum for this question. Given the following :
class Base1
{
public int i;
}
struct Base2
{
public int i;
}
class Program
{
static void Increment(Base1 b) { b.i++; }
static void Increment(Base2 b) { b.i++; }
static void Main(string[] args)
{
Base1 b1 = new Base1();
Base2 b2 = new Base2();
Increment(b1);
Increment(b2);
System.Console.WriteLine("b1 = " + b1.i + " and b2 = " + b2.i);
System.Console.ReadKey();
}
}
I know this produces:
b1 = 1 and b2 = 0
Why make the distinction between reference and value parameters implicit in
the case of classes versus structs, but explicit in the case of passing an
actual parameter to a formal paremeter declared with the 'ref' keyword? It's
not only explicit but required. That requirement would seem to lose a lot of
its value in light of the implicit distinction this example highlights.
So, what am I missing?
Hope this is the write forum for this question. Given the following :
class Base1
{
public int i;
}
struct Base2
{
public int i;
}
class Program
{
static void Increment(Base1 b) { b.i++; }
static void Increment(Base2 b) { b.i++; }
static void Main(string[] args)
{
Base1 b1 = new Base1();
Base2 b2 = new Base2();
Increment(b1);
Increment(b2);
System.Console.WriteLine("b1 = " + b1.i + " and b2 = " + b2.i);
System.Console.ReadKey();
}
}
I know this produces:
b1 = 1 and b2 = 0
Why make the distinction between reference and value parameters implicit in
the case of classes versus structs, but explicit in the case of passing an
actual parameter to a formal paremeter declared with the 'ref' keyword? It's
not only explicit but required. That requirement would seem to lose a lot of
its value in light of the implicit distinction this example highlights.
So, what am I missing?