C#6/7/8/9高级语法
2021/7/22 22:05:45
本文主要是介绍C#6/7/8/9高级语法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
源视频教程:https://www.bilibili.com/video/BV1tK4y1j7Eu
C#7~9版本新语法-官网:
C#7:https://docs.microsoft.com/zh-cn/dotnet/csharp/whats-new/csharp-7
C#8:https://docs.microsoft.com/zh-cn/dotnet/csharp/whats-new/csharp-8
C#9:https://docs.microsoft.com/zh-cn/dotnet/csharp/whats-new/csharp-9
一、C# 6新语法
1.1-自动属性初始化表达式
public class Student { public Student(string firstName, string lastName) { FirstName = firstName; LastName = lastName; } public int? Id { get; set; } public string FirstName { get; } public string LastName { get; } //对只读属性只能通过构造函数对其初始化值 //public void SetName(string firstName, string lastName) //{ // FirstName = firstName; // LastName = lastName; //} //自动属性初始化表达式 public ICollection<double> Grades { get; } = new List<double>(); //public override string ToString() => $"{LastName}, {FirstName}"; //public override string ToString() //{ // return $"{FirstName}-{LastName}"; //} public override string ToString()=> $"{FirstName}-{LastName}"; public string FullName => $"{FirstName}-{LastName}"; //goes to public string FullName1 { get { return string.Format("{0}-{1}", this.FirstName, this.LastName); } } public string TestName { get; set; } } //1.只读自动属性 { Student student = new Student("Richard", "Richard01"); string fullName = student.ToString(); string fullName1 = student.FullName; }
1.2-using static
using System; using System.Collections.Generic; using System.Text; namespace SharpGrammarInfo.SharpSixInfoTest { public class StaticClass { public static void Next() { Console.WriteLine("Next"); } public static void NextTo() { Console.WriteLine("NextTo"); } public static void ExceptionShow() { try { throw new Exception("教育"); } catch (Exception ex) when (ex.Message.Contains("教育")) //把异常的消息过滤,符合我的条件我就处理 { throw; } } } } //引入命名空间 using static SharpGrammarInfo.SharpSixInfoTest.StaticClass; //调用 //2.using static { StaticClass.Next(); StaticClass.NextTo(); Next(); NextTo(); }
1.3-Null 条件运算符
//3.Null 条件运算符 { Student student = null; // student.FirstName; //如果student为null 未将对象引用至对象的实例 //if (student!=null) //{ // string fullName = student.FullName; //} string fullName = student?.FullName; //得到的结果一定是要支持null int? id = student?.Id; //? 只能为可空类型服务 student = new Student("AB", "CD"); fullName = student?.FullName; string testName = student?.TestName ?? "朝夕教育"; student.TestName = "Zhaoxi教育"; testName = student?.TestName ?? "朝夕教育"; }
1.4-字符串内插
//4.字符串内插 { //string.Format(); //stringBuilder //string+ string firstName = "朝夕"; string lastName = "教育"; //输出{}转义 var strs = $"{{{firstName}}}-{lastName}-{firstName}-{firstName};{firstName}"; }
1.5-异常刷选器
public static void ExceptionShow() { try { throw new Exception("朝夕教育"); } //把异常的消息过滤,符合我的条件我就处理 catch (Exception ex) when (ex.Message.Contains("朝夕教育")) { throw; } } //5.异常筛选器 { 之前都是自定义不同的Exception 继承Exception父类 StaticClass.ExceptionShow(); }
1.6-nameof表达式
//6.nameof 表达式 { string clasName= "StaticClass"; string className1 = nameof(StaticClass); Console.WriteLine(className1); string className2 = nameof(SharpSixInfo); Console.WriteLine(className2); string className3 = nameof(Student); Console.WriteLine(className3); //输出类名字符串 }
1.7-事件(发布订阅模式)
using System.ComponentModel; namespace SharpGrammarInfo.SharpSix { public class NotifyPropertyChanged : INotifyPropertyChanged { public string LastName { get { return lastName; } set { if (value != lastName) { lastName = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(LastName))); } } } private string lastName; public event PropertyChangedEventHandler PropertyChanged; } } //7.事件(发布订阅模式) { ///什么是事件? NotifyPropertyChanged notifyPropertyChanged = new NotifyPropertyChanged(); notifyPropertyChanged.PropertyChanged += (object o, PropertyChangedEventArgs b) => { }; notifyPropertyChanged.LastName = "朝夕教育"; }
1.8-使用索引器初始化关联集合
//8.使用索引器初始化关联集合 { Dictionary<int, string> messages = new Dictionary<int, string> { { 404, "Page not Found"}, { 302, "Page moved, but left a forwarding address."}, { 500, "The web server can't come out to play today."} }; messages.Add(405,"朝夕教育"); messages.Remove(405, out string obj); Console.WriteLine(obj); Dictionary<int, string> webErrors = new Dictionary<int, string> { [404] = "Page not Found", [302] = "Page moved, but left a forwarding address.", [500] = "The web server can't come out to play today." }; webErrors.Add(405, "朝夕教育"); webErrors.Remove(405, out string strResult); }
这篇关于C#6/7/8/9高级语法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#
- 2024-01-24Advanced .Net Debugging 1:你必须知道的调试工具