C#演示結(jié)構(gòu)體的語(yǔ)法和用法的代碼

把做工程過程中比較常用的一些內(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);

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容