用户登录
用户注册

分享至

C# 6.0的属性(Property)的语法与初始值详解

  • 作者: 段子就是本黄书
  • 来源: 51数据库
  • 2022-04-04

昨晚有学点新知识,是有关c# 6.0的。

在数据库创建有一张表:

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

看看前后对比与写法:

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演示一下,创建一个实体:

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)的语法与初始值详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

软件
前端设计
程序设计
Java相关