C# 3.0新特性之自动属性(Auto-Implemented Properties)

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值