class and struct variables are confusing
// Look at comment below. Am I right?
using System;
using System.Collections.Generic;
using System.Text;
namespace Ch09Ex03
{
class Program
{
class MyClass
{
public int val;
}
struct myStruct
{
public int val;
}
static void Main(string[] args)
{
MyClass objectA = new MyClass();
MyClass objectB = objectA;
objectA.val = 10; /*Both objectA.val and objectB.val actually point to the same variable. So they will output the newest value 20.*/
objectB.val = 20;
myStruct structA = new myStruct();
myStruct structB = structA;
structA.val = 30; /*structA.val and structB.val are actually two variables. So they will output two different values: 30 and 40*/
structB.val = 40;
Console.WriteLine("objectA.val = {0}", objectA.val);
Console.WriteLine("objectB.val = {0}", objectB.val);
Console.WriteLine("structA.val = {0}", structA.val);
Console.WriteLine("structB.val = {0}", structB.val);
Console.ReadKey();
}
}
}
|