public class AutoPropertyBeforeCsharp6
{
private string _postTitle = string.Empty;
public AutoPropertyBeforeCsharp6()
{
//assign initial values
PostID = 1;
PostName = "Post 1";
}
public long PostID { get; set; }
public string PostName { get; set; }
public string PostTitle
{
get { return _postTitle; }
protected set
{
_postTitle = value;
}
}
}
public class AutoPropertyInCsharp6
{
public long PostID { get; } = 1;
public string PostName { get; } = "Post 1";
public string PostTitle { get; protected set; } = string.Empty;
}
public class PrimaryConstructorsBeforeCSharp6
{
public PrimaryConstructorsBeforeCSharp6(long postId, string postName, string postTitle)
{
PostID = postId;
PostName = postName;
PostTitle = postTitle;
}
public long PostID { get; set; }
public string PostName { get; set; }
public string PostTitle { get; set; }
}
public class PrimaryConstructorsInCSharp6(long postId, string postName, string postTitle)
{
public long PostID { get; } = postId;
public string PostName { get; } = postName;
public string PostTitle { get; } = postTitle;
}
<LangVersion>experimental</LangVersion>[url=http://venkatbaggu.com/wp-content/uploads/2014/10/Feature-primary-constructor-is-only-available-in-experimental-language-version.png][img]http://files.jb51.net/file_images/article/201503/2015032411183349.png[/img] [/url] ‘主构造器'只在‘实验'性质的语言版本中可用 [b]3. 字典初始化器 [/b] [b]之前的方式[/b] 编写一个字典初始化器的老办法如下
public class DictionaryInitializerBeforeCSharp6
{
public Dictionary<string, string> _users = new Dictionary<string, string>()
{
{"users", "Venkat Baggu Blog" },
{"Features", "Whats new in C# 6" }
};
}
public class DictionaryInitializerInCSharp6
{
public Dictionary<string, string> _users { get; } = new Dictionary<string, string>()
{
["users"] = "Venkat Baggu Blog",
["Features"] = "Whats new in C# 6"
};
}
public class DeclarationExpressionsBeforeCShapr6()
{
public static int CheckUserExist(string userId)
{
//Example 1
int id;
if (!int.TryParse(userId, out id))
{
return id;
}
return id;
}
public static string GetUserRole(long userId)
{
////Example 2
var user = _userRepository.Users.FindById(x => x.UserID == userId);
if (user!=null)
{
// work with address ...
return user.City;
}
}
}
public class DeclarationExpressionsInCShapr6()
{
public static int CheckUserExist(string userId)
{
if (!int.TryParse(userId, out var id))
{
return id;
}
return 0;
}
public static string GetUserRole(long userId)
{
////Example 2
if ((var user = _userRepository.Users.FindById(x => x.UserID == userId) != null)
{
// work with address ...
return user.City;
}
}
}
TypeName.MethodName
public class StaticUsingBeforeCSharp6
{
public void TestMethod()
{
Console.WriteLine("Static Using Before C# 6");
}
}
using System.Console;
namespace newfeatureincsharp6
{
public class StaticUsingInCSharp6
{
public void TestMethod()
{
WriteLine("Static Using Before C# 6");
}
}
}
try
{
//Do something
}
catch (Exception)
{
await Logger.Error("exception logging")
}
//示例 1
try
{
//Some code
}
catch (Exception ex) if (ex.InnerException == null)
{
//Do work
}
//Before C# 6 we write the above code as follows
//示例 1
try
{
//Some code
}
catch (Exception ex)
{
if(ex.InnerException != null)
{
//Do work;
}
}
var userRank = "No Rank";
if(UserID != null)
{
userRank = Rank;
}
//or
var userRank = UserID != null ? Rank : "No Rank"
var userRank = UserID?.Rank ?? "No Rank";以上所述就是本文的全部内容了,希望大家能够喜欢。
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-3 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2026 源码网商城 (www.yuanmawang.com) 版权所有