C# 3.0新特性之自动属性(Auto-Implemented Properties)
来源:互联网 发布:商家入驻源码 编辑:程序博客网 时间:2024/05/21 22:42
C# 3.0新特性之自动属性(Auto-Implemented Properties)
2009年8月18日 云飞扬 发表评论 阅读评论
-
万丈高楼平地起,基础是重中之重。所有我一定要搞点基础的东西,虽然已经是搞了几年程序了,有些基础知识也懂,但是没有系统的掌握。而且发现现在弄的B/S系统里很多技术真的很落后了,也许我现在学的新技术有些用不上,并不代表不要学,所有现在开始更加要全部重新学习或者复习一些基础东西。1.C# 3.0新特性之自动属性(Auto-Implemented Properties)类的定义public class Point{private int x;private int y;public int X { get { return x; } set { x = value; } }public int Y { get { return y; } set { y = value; } }}与下面这样定义等价,这就是c#新的特性public class Point{public int X {get; set;}public int Y {get; set;}}一个例子源码using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace NewLanguageFeatures1{public class Customer{public int CustomerId { get; private set; }public string Name { get; set; }public string City { get; set; }public override string ToString(){return Name + "/t" + City + "/t" + CustomerId;}}class Program{static void Main(string[] args){Customer c = new Customer();c.Name = "Alex Roland";c.City = "Berlin";c.CustomerId = 1;Console.WriteLine(c);}}}错误 1 由于 set 访问器不可访问,因此不能在此上下文中使用属性或索引器“NewLanguageFeatures1.Customer.CustomerId” D:/net/NewLanguageFeatures/NewLanguageFeatures1/Program.cs 41 13 NewLanguageFeatures1Program output showing the result of calling ToString on the Customer class after adding a new CustomerId property正确的例子源码:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace NewLanguageFeatures{public class Customer{public int CustomerId { get; private set; }public string Name { get; set; }public string City { get; set; }public Customer(int Id){CustomerId = Id;}public override string ToString(){return Name + "/t" + City + "/t" + CustomerId;}}class Program{static void Main(string[] args){Customer c = new Customer(1);c.Name = "Alex Roland";c.City = "Berlin";Console.WriteLine(c);}}}万丈高楼平地起,基础是重中之重。
所有我一定要搞点基础的东西,虽然已经是搞了几年程序了,有些基础知识也懂,但是没有系统的掌握。
而且发现现在弄的B/S系统里很多技术真的很落后了,也许我现在学的新技术有些用不上,并不代表不要学,
所有现在开始更加要全部重新学习或者复习一些基础东西。
1.C# 3.0新特性之自动属性(Auto-Implemented Properties)
类的定义
public class Point
{
private int x;
private int y;
public int X { get { return x; } set { x = value; } }
public int Y { get { return y; } set { y = value; } }
}
与下面这样定义等价,这就是c#新的特性
public class Point
{
public int X {get; set;}
public int Y {get; set;}
}
一个例子源码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NewLanguageFeatures1
{
public class Customer
{
public int CustomerId { get; private set; }
public string Name { get; set; }
public string City { get; set; }
public override string ToString()
{
return Name + "/t" + City + "/t" + CustomerId;
}
}
class Program
{
static void Main(string[] args)
{
Customer c = new Customer();
c.Name = "Alex Roland";
c.City = "Berlin";
c.CustomerId = 1;
Console.WriteLine(c);
}
}
}
错误 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
正确的例子源码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NewLanguageFeatures
{
public class Customer
{
public int CustomerId { get; private set; }
public string Name { get; set; }
public string City { get; set; }
public Customer(int Id)
{
CustomerId = Id;
}
public override string ToString()
{
return Name + "/t" + City + "/t" + CustomerId;
}
}
class Program
{
static void Main(string[] args)
{
Customer c = new Customer(1);
c.Name = "Alex Roland";
c.City = "Berlin";
Console.WriteLine(c);
}
}
}
本文来自: 本站内容欢迎转载,但是禁止去掉本文链接(转载无妨,去掉链接可耻!):http://www.ajaxcn.net/archives/171
- C# 3.0新特性之自动属性(Auto-Implemented Properties)
- C#3.0新特性系列:自动属性(Automatic Properties)
- 属性的自动实现(Auto-Implemented Properties) - VS2005
- 新的C#语言特性:自动属性(Automatic Properties)
- C# 3.0新特性之Automatic Properties(自动属性)、Object Initializers(对象初始化器)、Collection Initializers(集合初始化器)和Extension Methods(扩展方法)
- 步步为营VS 2008 + .NET 3.5(3) - C# 3.0新特性之Automatic Properties(自动属性)、Object Initializers(对象初始化器)、Collection Initializers(集合初始化器)和Ext
- 步步为营VS 2008 + .NET 3.5(3) - C# 3.0新特性之Automatic Properties(自动属性)、Object Initializers(对象初始化器)、Collection Initializers(集合初始化器)和Ext
- C# 3.0 的自动属性(Automatic Properties)
- C# 3.0 的自动属性(Automatic Properties)
- C# 3.0 的自动属性(Automatic Properties)
- C# 3.0 的自动属性(Automatic Properties)
- C# 3.0 的自动属性(Automatic Properties)
- C# 3.0 的自动属性(Automatic Properties)
- C#3.0新特性(三)-详说自动属性和匿名类型
- C#3.0新特性(三)- 详说自动属性和匿名类型
- C++ 11 新特性之 auto
- c++11 新特性之 auto关键字
- c++11新特性之auto
- Rails版本区别
- (1)Android介绍-Android示例大全教学视频
- Excel 导出
- ror
- C# 3.0新特性之对象和集合初始化
- C# 3.0新特性之自动属性(Auto-Implemented Properties)
- 修改Sybase数据库最大连接数
- 连接未关闭。连接的当前状态为已打开
- ASP.NET MVC – Create easy REST API with JSON and XML(转)
- Build distcc from source code
- Directory.EnumerateFiles的使用
- 基于MDC的P2P方式视频直播方案
- 虚拟光驱软件 daemon tools lite 4.4 下载 2011最新版
- QT 4.7 DBUS Binding如何支持自定义数据类型的传输(一)