国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#結構體特性實例分析

2019-10-29 21:38:05
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了C#結構體特性,以實例形式較為詳細的分析了C#結構體的功能、定義及相關特性,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了C#結構體特性。分享給大家供大家參考。具體如下:

結構體的定義:

結構體也可以象類一樣可以單獨定義.

 

 
  1. class a{}; 
  2. struct a{}; 

結構體也可以在名字前面加入控制訪問符.

 

 
  1. public struct student{}; 
  2. internal struct student{}; 

如果結構體student沒有publice或者internal的聲明 類program就無法使用student結構定義 obj對象

如果結構體student的元素沒有public的聲明,對象obj就無法調用元素x

因為默認的結構體名和元素名是*******類型

程序:

 

 
  1. using System; 
  2. public struct student 
  3. public int x; 
  4. }; 
  5. class program 
  6. public static void Main() 
  7. student obj=new student(); 
  8. obj.x=100;  
  9. }; 

在結構體中也可以定義靜態成員與類中一樣,使用時必須用類名,或結構名來調用不屬于實例,聲明時直接定義.

程序:

 

 
  1. using System; 
  2. public struct student 
  3. public static int a = 10; 
  4. }; 
  5. class exe 
  6. public static void Main() 
  7. Console.WriteLine( student.a = 100); 
  8. }; 

或:

 

  1. using System; 
  2. class base 
  3. public struct student 
  4. public static int a = 10; 
  5. }; 
  6. class exe 
  7. public static void Main() 
  8. Console.WriteLine( base.student.a = 100); 
  9. }; 

在結構體中可以定義構造函數以初始化成員,但不可以重寫默認無參構造函數和默認無參析構函數

程序:

 

 
  1. public struct student 
  2. public int x; 
  3. public int y; 
  4. public static int z; 
  5. public student(int a,int b,int c) 
  6. x=a; 
  7. y=b; 
  8. student.z=c; 
  9. }; 

在結構體中可以定義成員函數。

程序:

 

 
  1. public struct student 
  2. public void list() 
  3. Console.WriteLine("這是構造的函數"); 
  4. }; 

結構體的對象使用new運算符創建(obj)也可以直接創建單個元素賦值(obj2)這是與類不同的因為類只能使用new創建對象

程序:

 

 
  1. public struct student 
  2. public int x; 
  3. public int y; 
  4. public static int z; 
  5. public student(int a,int b,int c) 
  6. x=a; 
  7. y=b; 
  8. student.z=c; 
  9. }; 
  10. class program 
  11. public static void Main() 
  12. student obj=new student(100,200,300); 
  13. student obj2; 
  14. obj2.x=100; 
  15. obj2.y=200; 
  16. student.z=300; 

在使用類對象和函數使用時,使用的是引用傳遞,所以字段改變

在使用結構對象和函數使用時,是用的是值傳遞,所以字段沒有改變

程序:

 

 
  1. using System; 
  2. class class_wsy 
  3. public int x; 
  4. struct struct_wsy 
  5. public int x; 
  6. class program 
  7. public static void class_t(class_wsy obj) 
  8. obj.x = 90; 
  9. public static void struct_t(struct_wsy obj) 
  10. obj.x = 90; 
  11. public static void Main() 
  12. class_wsy obj_1 = new class_wsy(); 
  13. struct_wsy obj_2 = new struct_wsy(); 
  14. obj_1.x = 100; 
  15. obj_2.x = 100; 
  16. class_t(obj_1); 
  17. struct_t(obj_2); 
  18. Console.WriteLine("class_wsy obj_1.x={0}",obj_1.x); 
  19. Console.WriteLine("struct_wsy obj_2.x={0}",obj_2.x); 
  20. Console.Read(); 

結果為:

 

 
  1. class_wsy obj_1.x=90 
  2. struct_wsy obj_2.x=100 
 

 

 

希望本文所述對大家的C#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 蒙山县| 江川县| 潮安县| 丹江口市| 巴彦淖尔市| 莱芜市| 内乡县| 海晏县| 酒泉市| 万山特区| 黔西县| 西丰县| 当雄县| 石林| 齐河县| 宜宾市| 绩溪县| 稷山县| 龙州县| 商洛市| 调兵山市| 连江县| 阿瓦提县| 双桥区| 湛江市| 荆门市| 信丰县| 新巴尔虎左旗| 嘉荫县| 河间市| 佛坪县| 九龙城区| 海丰县| 鹿泉市| 威海市| 堆龙德庆县| 商水县| 鄂托克前旗| 阳江市| 兰西县| 山阴县|