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-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编码习惯命名规则

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#命名规约

2010-01-14 09:15:07

Java EE 6Servlet 3.0异步处理

2009-03-24 11:54:12

点赞
收藏

51CTO技术栈公众号