新聞中心
C#結(jié)構(gòu)體定義的情況:

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶,將通過(guò)不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:國(guó)際域名空間、雅安服務(wù)器托管、營(yíng)銷軟件、網(wǎng)站建設(shè)、南崗網(wǎng)站維護(hù)、網(wǎng)站推廣。
C#結(jié)構(gòu)體定義也可以象類一樣可以單獨(dú)定義.
- class a{};
- struct a{};
C#結(jié)構(gòu)體定義也可以在名字前面加入控制訪問(wèn)符.
- public struct student{};
- internal struct student{};
如果結(jié)構(gòu)體student沒(méi)有publice或者internal的聲明 類program就無(wú)法使用student結(jié)構(gòu)定義 obj對(duì)象
如果結(jié)構(gòu)體student的元素沒(méi)有public的聲明,對(duì)象obj就無(wú)法調(diào)用元素x
因?yàn)槟J(rèn)的結(jié)構(gòu)體名和元素名是private類型
C#結(jié)構(gòu)體定義之程序:
- using System;
- public struct student
- {
- public int x;
- };
- class program
- {
- public static void Main()
- {
- student obj=new student();
- obj.x=100;
- }
- };
在結(jié)構(gòu)體中也可以定義靜態(tài)成員與類中一樣,使用時(shí)必須用類名,或結(jié)構(gòu)名來(lái)調(diào)用不屬于實(shí)例,聲明時(shí)直接定義.
C#結(jié)構(gòu)體定義程序:
- using System;
- public struct student
- {
- public static int a = 10;
- };
- class exe
- {
- public static void Main()
- {
- Console.WriteLine( student.a = 100);
- }
- };
或
- using System;
- class base
- {
- public struct student
- {
- public static int a = 10;
- };
- }
- class exe
- {
- public static void Main()
- {
- Console.WriteLine( base.student.a = 100);
- }
- };
在結(jié)構(gòu)體中可以定義構(gòu)造函數(shù)以初始化成員,但不可以重寫(xiě)默認(rèn)無(wú)參構(gòu)造函數(shù)和默認(rèn)無(wú)參析構(gòu)函數(shù)
C#結(jié)構(gòu)體定義程序:
- public struct student
- {
- public int x;
- public int y;
- public static int z;
- public student(int a,int b,int c)
- {
- x=a;
- y=b;
- student.z=c;
- }
- };
在結(jié)構(gòu)體中可以定義成員函數(shù)。
C#結(jié)構(gòu)體定義程序:
- public struct student
- {
- public void list()
- {
- Console.WriteLine("這是構(gòu)造的函數(shù)");
- }
- };
結(jié)構(gòu)體的對(duì)象使用new運(yùn)算符創(chuàng)建(obj)也可以直接創(chuàng)建單個(gè)元素賦值(obj2)這是與類不同的因?yàn)轭愔荒苁褂胣ew創(chuàng)建對(duì)象
C#結(jié)構(gòu)體定義程序:
- public struct student
- {
- public int x;
- public int y;
- public static int z;
- public student(int a,int b,int c)
- {
- x=a;
- y=b;
- student.z=c;
- }
- };
- class program
- {
- public static void Main()
- {
- student obj=new student(100,200,300);
- student obj2;
- obj2.x=100;
- obj2.y=200;
- student.z=300;
- }
- }
在使用類對(duì)象和函數(shù)使用時(shí),使用的是引用傳遞,所以字段改變
在使用結(jié)構(gòu)對(duì)象和函數(shù)使用時(shí),是用的是值傳遞,所以字段沒(méi)有改變
C#結(jié)構(gòu)體定義程序:
- using System;
- class class_wsy
- {
- public int x;
- }
- struct struct_wsy
- {
- public int x;
- }
- class program
- {
- public static void class_t(class_wsy obj)
- {
- obj.x = 90;
- }
- public static void struct_t(struct_wsy obj)
- {
- obj.x = 90;
- }
- public static void Main()
- {
- class_wsy obj_1 = new class_wsy();
- struct_wsy obj_2 = new struct_wsy();
- obj_1.x = 100;
- obj_2.x = 100;
- class_t(obj_1);
- struct_t(obj_2);
- Console.WriteLine("class_wsy obj_1.x={0}",obj_1.x);
- Console.WriteLine("struct_wsy obj_2.x={0}",obj_2.x);
- Console.Read();
- }
- }
C#結(jié)構(gòu)體定義程序運(yùn)行結(jié)果為:
- class_wsy obj_1.x=90
- struct_wsy obj_2.x=100
C#結(jié)構(gòu)體定義的基本內(nèi)容就向你介紹到這里,希望對(duì)你了解C#結(jié)構(gòu)體定義有所幫助。
網(wǎng)頁(yè)標(biāo)題:C#結(jié)構(gòu)體定義的詳解
文章分享:http://fisionsoft.com.cn/article/dpigedg.html


咨詢
建站咨詢
