using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StructAndClass
{
class Program
{
interface ITestInterface
{
void Paint();
}
// define a struct
public struct TestStruct: ITestInterface
{
public string Name;
public int height;
public bool overWeight()
{
return true;
}
public void Paint() { }
}
// define a class
public class TestClass: ITestInterface
{
public string Name = "Time";
public void Paint() { }
~TestClass()
{
Console.WriteLine("Test class destructor.");
}
}
public sealed class ClassExtend: TestClass
{
public ClassExtend()
{
Console.WriteLine("Test class is extended. However, this class can not be extended.");
}
}
static void Main(string[] args)
{
//TestStruct structOne = new TestStruct();
TestStruct structOne;
structOne.Name = "Tim";
structOne.height = 10;
TestClass classOne = new TestClass();
// error
//TestClass classOne;
//classOne.Name = "Tim";
Console.WriteLine("Struct's base class is object: " + ((structOne is object) ? "Yes" : "No"));
Console.WriteLine("Class's base class is object: " + ((classOne is object) ? "Yes" : "No"));
ClassExtend classExtend = new ClassExtend();
structOne.Name = "Tim Changed";
classOne.Name = "Tim Changed";
Console.WriteLine("Test to change struct variable,name is changed: " + (structOne.Name == "Tim Changed" ? "Yes" : "No") + " Name: " + structOne.Name);
Console.WriteLine("Test to change class variable, name is changed: " + (classOne.Name == "Tim Changed" ? "Yes" : "No") + " Name: " + classOne.Name);
Console.ReadKey();
}
}
}