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)
THE END