把做工程過程中比較常用的一些內(nèi)容片段記錄起來(lái),下面的內(nèi)容段是關(guān)于C#演示結(jié)構(gòu)體的語(yǔ)法和用法的內(nèi)容,希望對(duì)小伙伴們也有好處。
using System;
struct SimpleStruct
{
? ? private int xval;
? ? public int X
? ? {
? ? ? ? get
? ? ? ? {
? ? ? ? ? ? return xval;
? ? ? ? }
? ? ? ? set
? ? ? ? {
? ? ? ? ? ? if (value < 100)
? ? ? ? ? ? ? ? xval = value;
? ? ? ? }
? ? }
? ? public void DisplayX()
? ? {
? ? ? ? Console.WriteLine("The stored value is: {0}", xval);
? ? }
}
class TestClass
{
? ? public static void Main()
? ? {
? ? ? ? SimpleStruct ss = new SimpleStruct();
? ? ? ? ss.X = 5;
? ? ? ? ss.DisplayX();
? ? }
}
using System;
class TheClass
{
? ? public int x;
}
struct TheStruct
{
? ? public int x;
}
class TestClass
{
? ? public static void structtaker(TheStruct s)
? ? {
? ? ? ? s.x = 5;
? ? }
? ? public static void classtaker(TheClass c)
? ? {
? ? ? ? c.x = 5;
? ? }
? ? public static void Main()
? ? {
? ? ? ? TheStruct a = new TheStruct();
? ? ? ? TheClass b = new TheClass();
? ? ? ? a.x = 1;
? ? ? ? b.x = 1;
? ? ? ? structtaker(a);
? ? ? ? classtaker(b);
? ? ? ? Console.WriteLine("a.x = {0}", a.x);
? ? ? ? Console.WriteLine("b.x = {0}", b.x);
? ? }
}