C#結(jié)構(gòu)體特性實例分析
本文實例講述了C#結(jié)構(gòu)體特性。分享給大家供大家參考。具體如下:
結(jié)構(gòu)體的定義:
結(jié)構(gòu)體也可以象類一樣可以單獨(dú)定義.
class a{}; struct a{};
結(jié)構(gòu)體也可以在名字前面加入控制訪問符.
public struct student{}; internal struct student{};
如果結(jié)構(gòu)體student沒有publice或者internal的聲明 類program就無法使用student結(jié)構(gòu)定義 obj對象
如果結(jié)構(gòu)體student的元素沒有public的聲明,對象obj就無法調(diào)用元素x
因為默認(rèn)的結(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)成員與類中一樣,使用時必須用類名,或結(jié)構(gòu)名來調(diào)用不屬于實例,聲明時直接定義.
程序:
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ù)以初始化成員,但不可以重寫默認(rèn)無參構(gòu)造函數(shù)和默認(rèn)無參析構(gòu)函數(shù)
程序:
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ù)。
程序:
public struct student { public void list() { Console.WriteLine("這是構(gòu)造的函數(shù)"); } };
結(jié)構(gòu)體的對象使用new運(yùn)算符創(chuàng)建(obj)也可以直接創(chuàng)建單個元素賦值(obj2)這是與類不同的因為類只能使用new創(chuàng)建對象
程序:
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; } }
在使用類對象和函數(shù)使用時,使用的是引用傳遞,所以字段改變
在使用結(jié)構(gòu)對象和函數(shù)使用時,是用的是值傳遞,所以字段沒有改變
程序:
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(); } }
結(jié)果為:
class_wsy obj_1.x=90 struct_wsy obj_2.x=100
希望本文所述對大家的C#程序設(shè)計有所幫助。
欄 目:C#教程
下一篇:C#中實現(xiàn)一次執(zhí)行多條帶GO的sql語句實例
本文標(biāo)題:C#結(jié)構(gòu)體特性實例分析
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6939.html
您可能感興趣的文章
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并打開的方法
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10winform實現(xiàn)創(chuàng)建最前端窗體的方法
- 01-10C#實現(xiàn)實體類與字符串互相轉(zhuǎn)換的方法
- 01-10C#實現(xiàn)由四周向中心縮小的窗體退出特效
- 01-10C#實現(xiàn)主窗體最小化后出現(xiàn)懸浮框及雙擊懸浮框恢復(fù)原窗體的方
- 01-10C#實現(xiàn)更改MDI窗體背景顏色的方法
- 01-10C#實現(xiàn)打開畫圖的同時載入圖片、最大化顯示畫圖窗體的方法
- 01-10C#實現(xiàn)將窗體固定在顯示器的左上角且不能移動的方法
- 01-10C#禁用雙擊窗體圖標(biāo)關(guān)閉窗體的方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻 器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實例總結(jié)
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-11ajax實現(xiàn)頁面的局部加載