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

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 钛钢玫瑰金变黑怎么办 玫瑰金手镯掉色了怎么办 彩金颜色不亮了怎么办 玫瑰金链子黑了怎么办 18k玫瑰金变黑了怎么办 玫瑰金戒指遇到火变黑怎么办 18k白金发黄了怎么办 18k金掉色后怎么办吗 dw手表金色掉漆怎么办 dw玫瑰金手表褪色怎么办 dw金色表带黑了怎么办 机械表机芯坏了怎么办 银镀玫瑰金褪色怎么办 苹果七p玫瑰金掉漆怎么办 美度镀金表掉色怎么办 吃了发黑的香菇怎么办 脸上的皮肤暗黄怎么办 吃了变黑的香菇怎么办 怀孕喝了玫瑰茶怎么办 孕妇爱喝茉莉茶怎么办 干菊花生虫了该怎么办 黑枸杞放了两年怎么办 来大姨妈喝茶了怎么办 干柠檬片过期了怎么办 山地玫瑰根黑了怎么办 带18k的金过敏怎么办 18k钻戒断了怎么办 18k钻戒戒托变色怎么办 苹果七中间按键脱漆怎么办 苹果手机屏幕边缘掉漆了怎么办 十八k白金变色了怎么办 手机后面掉漆了怎么办 玫瑰金戒指刮花了怎么办 玫瑰金手镯刮花了怎么办 玫瑰金链子断了怎么办 dw玫瑰金掉色了怎么办 6s玫瑰金氧化了怎么办 书本湿的很严重怎么办 除湿盒的水满了怎么办 书被水泡变黄了怎么办 文件纸被弄皱了怎么办