成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

C#3.0新特性的介紹(自動屬性)

開發 后端
本文從類的定義開始,介紹C# 3.0新特性中的自動屬性特性,并且舉例說明,供大家參考啊。

萬丈高樓平地起,基礎是重中之重。

所有我一定要搞點基礎的東西,雖然已經是搞了幾年程序了,有些基礎知識也懂,但是沒有系統的掌握。

而且發現現在弄的B/S系統里很多技術真的很落后了,也許我現在學的新技術有些用不上,并不代表不要學,

所有現在開始更加要全部重新學習或者復習一些基礎東西。

C# 3.0新特性之自動屬性(Auto-Implemented Properties)

類的定義

  1. public class Point  
  2. {  
  3.     private int x;  
  4.     private int y;  
  5.  
  6.     public int X { get { return x; } set { x = value; } }  
  7.     public int Y { get { return y; } set { y = value; } }  

與下面這樣定義等價,這就是c#3.0新特性

  1. public class Point  
  2. {  
  3.     public int X {getset;}  
  4.     public int Y {getset;}  
  5. }  
  6.  
  7. 一個例子源碼  
  8. using System;  
  9. using System.Collections.Generic;  
  10. using System.Linq;  
  11. using System.Text;  
  12.  
  13. namespace NewLanguageFeatures1  
  14. {  
  15.     public class Customer  
  16.     {  
  17.         public int CustomerId { getprivate set; }  
  18.         public string Name { getset; }  
  19.         public string City { getset; }  
  20.  
  21.         public override string ToString()  
  22.         {  
  23.             return Name + “\t” + City + “\t” + CustomerId;  
  24.         }  
  25.  
  26.     }  
  27.     class Program  
  28.     {  
  29.         static void Main(string[] args)  
  30.         {  
  31.             Customer c = new Customer();  
  32.             c.Name = “Alex Roland”;  
  33.             c.City = “Berlin”;  
  34.             c.CustomerId = 1;  
  35.  
  36.             Console.WriteLine(c);  
  37.  
  38.         }  
  39.  
  40.          
  41.     }  

錯誤 1 由于 set 訪問器不可訪問,因此不能在此上下文中使用屬性或索引器“NewLanguageFeatures1.Customer.CustomerId” D:\net\NewLanguageFeatures\NewLanguageFeatures1\Program.cs 41 13 NewLanguageFeatures1

Program output showing the result of calling ToString on the Customer class after adding a new CustomerId property

正確的例子源碼:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.  
  6. namespace NewLanguageFeatures  
  7. {  
  8.     public class Customer  
  9.     {  
  10.         public int CustomerId { getprivate set; }  
  11.  
  12.         public string Name { getset; }  
  13.         public string City { getset; }  
  14.  
  15.         public Customer(int Id)  
  16.         {  
  17.             CustomerId = Id;  
  18.         }  
  19.  
  20.         public override string ToString()  
  21.         {  
  22.             return Name + “\t” + City + “\t” + CustomerId;  
  23.         }  
  24.     }  
  25.  
  26.     class Program  
  27.     {  
  28.         static void Main(string[] args)  
  29.         {  
  30.             Customer c = new Customer(1);  
  31.             c.Name = “Alex Roland”;  
  32.             c.City = “Berlin”;  
  33.  
  34.             Console.WriteLine(c);  
  35.         }  
  36.     }  

關于C#3.0新特性的自動屬性功能就討論到這里,希望對大家有用。

【編輯推薦】

  1. C#控制臺應用程序的基本結構
  2. C#編程:使用迭代器
  3. 淺談C#泛型的定義、繼承、方法和約束
  4. C++和C#相互調用COM組件的方法簡介
  5. 如何實現C#代理(Delegate)
責任編輯:book05 來源: ajaxcn
相關推薦

2009-04-23 17:56:05

C#自動屬性對象初始化

2009-08-31 14:45:07

Visual C# 3

2009-08-27 16:24:48

擴展方法C# 3.0新特性

2009-08-18 17:03:49

C#3.5新特性

2009-08-24 18:01:45

C#3.0新特性

2009-08-12 13:15:44

C#3.5新特性

2009-08-19 16:51:14

C# 4.0 dyna

2009-03-11 10:06:42

C#3.0編碼習慣命名規則

2009-07-01 09:56:10

C#3.0

2009-07-27 09:46:28

Silverlight

2011-07-27 16:12:35

Linux KerneLinux內核

2009-07-08 09:35:53

Java ServleServlet 3.0

2025-05-07 03:15:00

NacosAPIMCP

2021-04-30 19:53:41

Java表達式代碼

2012-03-14 12:29:55

JavaPlay Framwo

2009-08-28 08:46:15

Windows 7防火墻

2009-09-18 15:53:37

C# 3.0新語言特性

2009-08-04 08:48:44

C#內置特性

2009-09-01 17:29:51

C#命名規約

2011-07-06 16:38:57

Xcode Preview
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 男女羞羞视频免费 | 日韩中文字幕在线观看 | 亚洲视频免费在线播放 | 亚洲精品资源 | 欧美日韩不卡 | 欧美日韩综合 | 亚洲精品一区二区三区蜜桃久 | 久久亚洲精品久久国产一区二区 | 精品日韩在线观看 | 欧美日本韩国一区二区三区 | 91精品国产91久久久久游泳池 | 一级国产精品一级国产精品片 | 欧美国产一区二区 | 久久久一二三 | 日本视频免费观看 | 三级国产三级在线 | 日韩一区二区在线视频 | 求个av网址 | 日韩视频在线播放 | 天天干天天操 | 九九久久精品 | 91视频正在播放 | 色综合激情 | 啪啪免费网站 | 日韩黄色免费 | 亚洲一区二区三区视频免费观看 | 国产黄色一级片 | 日韩影院一区 | 亚洲一区二区三区免费观看 | 天天久久 | 国产精品久久久久久久7电影 | 国产91视频免费 | av影音资源 | 国产91在线观看 | 欧美大片一区二区 | 国产一区二区三区色淫影院 | 日本特黄a级高清免费大片 成年人黄色小视频 | 91久久久久久 | 亚洲成人天堂 | 国产乱精品一区二区三区 | 99热这里都是精品 |