昨晚有学点新知识,是有关c# 6.0的。
在数据库创建有一张表:
| 1 2 3 4 5 6 7 8 9 |
create table [dbo].[toollocation]
(
[toollocation_nbr] smallint identity(1,1) not null primary key,
[locationname] nvarchar(20) not null,
[description] nvarchar(50) null,
[isactive] bit not null default(1)
)
go
source code
|
看看前后对比与写法:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;
namespace insus.net.models
{
public class toollocation
{
public short toollocation_nbr { get; set; } = 1;
public string locationname { get; set; } = string.empty;
public string description { get; set; } = string.empty;
public bool isactive { get; set; } = true;
}
}
source code
|
下面insus.net演示一下,创建一个实体:

| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
using insus.net.models;
using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;
namespace insus.net.entities
{
public class toollocationentity
{
public ienumerable<toollocation> toollocations()
{
return new list<toollocation>() {
new toollocation(),
new toollocation { toollocation_nbr = 2, locationname = "a2", description = "a2 cnc",isactive = true},
new toollocation { toollocation_nbr = 3, locationname = "c4", description = "c4 cnc",isactive = false}
};
}
}
}
source code
|
它将会有三个对象,第一个对象是使用默认值。
在控制器中:
在asp.net mvc视图中,显示这些数据:
看看运行的效果:
以上这篇c# 6.0的属性(property)的语法与初始值详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。








发表评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。