C# 6.0的屬性(Property)的語法與初始值詳解
昨晚有學(xué)點(diǎn)新知識,是有關(guān)C# 6.0的。
在數(shù)據(jù)庫創(chuàng)建有一張表:
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演示一下,創(chuàng)建一個(gè)實(shí)體:
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
它將會有三個(gè)對象,第一個(gè)對象是使用默認(rèn)值。
在控制器中:
在ASP.NET MVC視圖中,顯示這些數(shù)據(jù):
看看運(yùn)行的效果:
以上這篇C# 6.0的屬性(Property)的語法與初始值詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
unity實(shí)現(xiàn)車方向盤轉(zhuǎn)動效果
這篇文章主要為大家詳細(xì)介紹了unity實(shí)現(xiàn)車方向盤轉(zhuǎn)動效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04C#實(shí)現(xiàn)多選項(xiàng)卡的瀏覽器控件
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)多選項(xiàng)卡的瀏覽器控件的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-03-03c#讀取圖像保存到數(shù)據(jù)庫中(數(shù)據(jù)庫保存圖片)
這篇文章主要介紹了使用c#讀取圖像保存到數(shù)據(jù)庫中的方法,大家參考使用吧2014-01-01使用Deflate算法對文件進(jìn)行壓縮與解壓縮的方法詳解
本篇文章是對使用Deflate算法對文件進(jìn)行壓縮和解壓縮的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06