zoukankan
html css js c++ java
.Net3.0中的自动属性(示例)
using
System;
namespace
LinqDemo
{
class
Program
{
static
void
Main(
string
[] args)
{
//
传统用法示例
Employee Emp
=
new
Employee(
"
Jimmy.Yang
"
,
25
);
Console.WriteLine(Emp.ToString());
Console.WriteLine(
"
-------------------
"
);
//
自动属性的写法
NewEmployee NewEmp
=
new
NewEmployee
{ Name
=
"
Tom
"
, Age
=
30
}
;
//
感觉与Javascript中对象的JSON字符串表示法相似
Console.WriteLine(NewEmp.ToString());
Console.ReadLine();
}
}
/**/
///
<summary>
///
传统方式定义一个类
///
</summary>
public
class
Employee
{
private
string
_name
=
"
Anonymous
"
;
private
int
_age
=
0
;
public
string
Name
{
get
{
return
this
._name; }
set
{
this
._name
=
value; }
}
public
int
Age
{
get
{
return
this
._age; }
set
{
this
._age
=
value; }
}
public
Employee()
{ }
public
Employee(
string
pName,
int
pAge)
{
this
._name
=
pName;
this
._age
=
pAge;
}
public
override
string
ToString()
{
return
"
Name:
"
+
this
._name
+
"
Age:
"
+
this
._age;
}
}
/**/
///
<summary>
///
.Net3.0自动属性的新写法
///
</summary>
public
class
NewEmployee
{
public
string
Name
{
get
;
set
;}
public
int
Age
{
get
;
set
; }
public
override
string
ToString()
{
return
"
Name:
"
+
this
.Name
+
"
Age:
"
+
this
.Age;
}
}
}
可以看出,.Net3.0的自动属性,可以使定义一个类的代码大大减化,个人感觉:这一点好象又是从Javascript中的JSON字符串表示法“偷”来的^_^,不信的话,可以参看以下Javascript代码:
<
script type
=
"
text/javascript
"
>
var
Emp
=
{Name:
"
Jimmy.Yang
"
,Age:
30
}
;
function
ShowEmp(E)
{
return
"
Name:
"
+
E.Name
+
"
Age:
"
+
E.Age;
}
document.write(ShowEmp(Emp));
<
/
script>
顺便发表一下个人感慨:微软确实很善于吸引他人长处
作者:
菩提树下的杨过
出处:
http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
查看全文
相关阅读:
Active Url 激活URL
Specified argument was out of the range of valid values.
EnumHelp
历史对像(版本对像)
dom4j API 简介
即时通讯软件openfire+spark+smack
openfire插件入门学习
Openfire本地化配置
JSONObject与JSONArray的使用
Android端服务器推送技术原理分析及XMPP的使用
原文地址:https://www.cnblogs.com/yjmyzz/p/1063912.html
最新文章
做一个健康的IT人
创建安全的便于记忆的密码
做一个健康的IT人
求职问答:世界五百强面试题目及应答评点[转贴]
CPU介绍(2)
C#中隐藏(new)和方法重写(override)和重载(overload)的区别
检索 COM 类工厂中 CLSID 为 {10020200E26011CFAE6800AA004A34D5} 的组件时失败,解决方法如下:
char、varchar、text和nchar、nvarchar、ntext的区别
CSS101文章学习
CollectionBase集合的基类
热门文章
[转]什么是家?
CSS的技巧
记录点滴(一)
数据库范式
使用Aforge.net进行颜色替换
DirectSmtpClient 邮件直接发送 (跳过邮件中转服务器)
正则非匹配
The mapping of interface member IDisplayOrder.ID is not supported.
定时触发问题
最近写了一个C#的银江接口 (股票数据接口)
Copyright © 2011-2022 走看看