自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

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

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

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

而且發(fā)現(xiàn)現(xiàn)在弄的B/S系統(tǒng)里很多技術真的很落后了,也許我現(xiàn)在學的新技術有些用不上,并不代表不要學,

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

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. 如何實現(xiàn)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-24 18:01:45

C#3.0新特性

2009-08-18 17:03:49

C#3.5新特性

2009-03-11 10:06:42

C#3.0編碼習慣命名規(guī)則

2009-07-01 09:56:10

C#3.0

2009-08-19 16:51:14

C# 4.0 dyna

2009-08-12 13:15:44

C#3.5新特性

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

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#命名規(guī)約

2009-03-24 11:54:12

2021-03-06 08:10:16

Redis6 Java架構分布式框架
點贊
收藏

51CTO技術棧公眾號