51 lines
1.2 KiB
Plaintext
51 lines
1.2 KiB
Plaintext
// Filename: varsexample.cs
|
|
// Defining value and reference types in C#
|
|
|
|
struct ValType {
|
|
public int i;
|
|
public double d;
|
|
public ValType(int i, double d) {
|
|
this.i = i;
|
|
this.d = d;
|
|
}
|
|
public override string ToString() {
|
|
return "(" + i + ", " + d + ")";
|
|
}
|
|
}
|
|
|
|
class RefType {
|
|
public int i;
|
|
public double d;
|
|
public RefType(int i, double d) {
|
|
this.i = i;
|
|
this.d = d;
|
|
}
|
|
public override string ToString() {
|
|
return "(" + i + ", " + d + ")";
|
|
}
|
|
}
|
|
|
|
public class Test {
|
|
public static void Main (string[] args) {
|
|
|
|
// PART 1
|
|
ValType v1;
|
|
RefType r1;
|
|
v1 = new ValType(3, 4.2);
|
|
r1 = new RefType(4, 5.1);
|
|
System.Console.WriteLine("PART 1");
|
|
System.Console.WriteLine("v1 = " + v1);
|
|
System.Console.WriteLine("r1 = " + r1);
|
|
|
|
// PART 2
|
|
ValType v2;
|
|
RefType r2;
|
|
v2 = v1;
|
|
r2 = r1;
|
|
v2.i++; v2.d++;
|
|
r2.i++; r2.d++;
|
|
System.Console.WriteLine("PART 2");
|
|
System.Console.WriteLine("v1 = " + v1);
|
|
System.Console.WriteLine("r1 = " + r1);
|
|
}
|
|
} |