偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

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

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

萬丈高樓平地起,基礎(chǔ)是重中之重。

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

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

所有現(xiàn)在開始更加要全部重新學(xué)習(xí)或者復(fù)習(xí)一些基礎(chǔ)東西。

C# 3.0新特性之自動(dòng)屬性(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; } }  

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

  1. public class Point  
  2. {  
  3.     public int X {getset;}  
  4.     public int Y {getset;}  
  5. }  
  6.  
  7. 一個(gè)例子源碼  
  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.     }  

錯(cuò)誤 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.     }  

關(guān)于C#3.0新特性的自動(dòng)屬性功能就討論到這里,希望對(duì)大家有用。

【編輯推薦】

  1. C#控制臺(tái)應(yīng)用程序的基本結(jié)構(gòu)
  2. C#編程:使用迭代器
  3. 淺談C#泛型的定義、繼承、方法和約束
  4. C++和C#相互調(diào)用COM組件的方法簡(jiǎn)介
  5. 如何實(shí)現(xiàn)C#代理(Delegate)
責(zé)任編輯:book05 來源: ajaxcn
相關(guān)推薦

2009-04-23 17:56:05

C#自動(dòng)屬性對(duì)象初始化

2009-08-31 14:45:07

Visual C# 3

2009-08-27 16:24:48

擴(kuò)展方法C# 3.0新特性

2009-08-18 17:03:49

C#3.5新特性

2009-08-24 18:01:45

C#3.0新特性

2009-03-11 10:06:42

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

2009-07-01 09:56:10

C#3.0

2009-08-12 13:15:44

C#3.5新特性

2009-08-19 16:51:14

C# 4.0 dyna

2009-07-27 09:46:28

Silverlight

2011-07-27 16:12:35

Linux KerneLinux內(nèi)核

2009-07-08 09:35:53

Java ServleServlet 3.0

2025-05-07 03:15:00

NacosAPIMCP

2021-04-30 19:53:41

Java表達(dá)式代碼

2012-03-14 12:29:55

JavaPlay Framwo

2009-08-28 08:46:15

Windows 7防火墻

2009-08-04 08:48:44

C#內(nèi)置特性

2009-09-18 15:53:37

C# 3.0新語(yǔ)言特性

2009-09-01 17:29:51

C#命名規(guī)約

2011-07-06 16:38:57

Xcode Preview
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)