新聞中心
萬(wàn)丈高樓平地起,基礎(chǔ)是重中之重。

創(chuàng)新互聯(lián)公司是專業(yè)的羅源網(wǎng)站建設(shè)公司,羅源接單;提供網(wǎng)站制作、網(wǎng)站建設(shè),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行羅源網(wǎng)站開發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
所有我一定要搞點(diǎn)基礎(chǔ)的東西,雖然已經(jīng)是搞了幾年程序了,有些基礎(chǔ)知識(shí)也懂,但是沒(méi)有系統(tǒng)的掌握。
而且發(fā)現(xiàn)現(xiàn)在弄的B/S系統(tǒng)里很多技術(shù)真的很落后了,也許我現(xiàn)在學(xué)的新技術(shù)有些用不上,并不代表不要學(xué),
所有現(xiàn)在開始更加要全部重新學(xué)習(xí)或者復(fù)習(xí)一些基礎(chǔ)東西。
C# 3.0新特性之自動(dòng)屬性(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; } }
- }
與下面這樣定義等價(jià),這就是c#3.0新特性
- public class Point
- {
- public int X {get; set;}
- public int Y {get; set;}
- }
- 一個(gè)例子源碼
- 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);
- }
- }
- }
錯(cuò)誤 1 由于 set 訪問(wèn)器不可訪問(wèn),因此不能在此上下文中使用屬性或索引器“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);
- }
- }
- }
關(guān)于C#3.0新特性的自動(dòng)屬性功能就討論到這里,希望對(duì)大家有用。
網(wǎng)站題目:C#3.0新特性的介紹(自動(dòng)屬性)
當(dāng)前網(wǎng)址:http://www.dlmjj.cn/article/cdegjoc.html


咨詢
建站咨詢
