C# 6新语法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C# 6新语法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3875字,纯文字阅读大概需要6分钟。
内容图文
![C# 6新语法](/upload/InfoBanner/zyjiaocheng/601/cc8f994f88a541909d9f8854a7c7e977.jpg)
![C# 6新语法 - 文章图片](/upload/getfiles/0001/2021/4/30/20210430014804870.jpg)
![C# 6新语法 - 文章图片](/upload/getfiles/0001/2021/4/30/20210430014804989.jpg)
1 using System; 2 3 namespace ConsoleApp1 4 { 5 class Program 6 { 7 static void Main(string[] args) 8 { 9 10 { 11 Student student = new Student("蛋蛋", "张"); 12 13 Console.WriteLine(student.ToString()); 14 15 student.Show(); 16 } 17 18 { 19 //条件运算符 20 Student student1 = null; 21 22 //1. 得到的结果一定要支持null 23 //2. 如果 student1 是 null,返回 null,如果 student1 有值 就返回 student1.FirstName 24 string fullName = student1?.FirstName; 25 26 //必须为可空类型,如果是 int id = student1?.Id; 则报错 27 int? id = student1?.Id; 28 29 Console.WriteLine("Hello World!"); 30 31 student1 = new Student("AA", "BB"); 32 fullName = student1?.FullName1; 33 34 //1. 判断student1是否为null,如果为null,直接返回null; 35 //2. 如果不为null,判断 student1.FullName1 是否为null,如果为null,返回“张三”,如果不为null,返回 student1.FullName1 36 string testName = student1?.TestName ?? "张三"; 37 } 38 39 //字符串内插 40 { 41 string firstName = "蛋蛋"; 42 string lastName = "张"; 43 string strs = $"{{{lastName}}}-{firstName}"; //结果:{张}-蛋蛋 44 } 45 46 //异常筛选器 47 { 48 try 49 { 50 throw new Exception("我是张蛋蛋的好朋友!"); 51 }catch(Exception ex) when(ex.Message.Contains("张蛋蛋")) 52 { 53 Console.WriteLine("如果异常Message字符串中包含‘张蛋蛋’,那么就会显示此段话!"); 54 //throw; 55 } 56 } 57 //nameof 表达式 58 { 59 60 string className = nameof(Student); 61 Console.WriteLine($"获取到类名称:{className}"); 62 } 63 64 } 65 } 66 }Program 类
![C# 6新语法 - 文章图片](/upload/getfiles/0001/2021/4/30/20210430014804870.jpg)
![C# 6新语法 - 文章图片](/upload/getfiles/0001/2021/4/30/20210430014804989.jpg)
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 5 namespace ConsoleApp1 6 { 7 public class Student 8 { 9 public Student(string firstName,string lastName) 10 { 11 FirstName = firstName; 12 LastName = lastName; 13 } 14 15 public int? Id { get; set; } 16 public string TestName { get; set; } 17 18 /// <summary> 19 /// 只能通过构造函数赋值 20 /// </summary> 21 public string FirstName { get; } 22 public string LastName { get; } 23 24 /// <summary> 25 /// FullName1 与 FullName2 功能相同 26 /// </summary> 27 public string FullName1 28 { 29 get 30 { 31 return string.Format("{0}-{1}", this.FirstName, this.LastName); 32 } 33 } 34 35 public string FullName2=> string.Format("{0}-{1}", this.FirstName, this.LastName);//念 goes to 36 37 public void Show() 38 { 39 Console.WriteLine(string.Format("FullName1:{0}",FullName1)); 40 Console.WriteLine(string.Format("FullName2:{0}", FullName2)); 41 } 42 43 /// <summary> 44 /// 方法A (方法A与方法B功能相同) 45 /// </summary> 46 /// <returns></returns> 47 public override string ToString() => string.Format("我是方法:{0}-{1}", this.FirstName, this.LastName);//念 goes to 48 /// <summary> 49 /// 方法B 50 /// </summary> 51 /// <returns></returns> 52 //public override string ToString() 53 //{ 54 // return string.Format("我是方法:{0}-{1}", this.FirstName, this.LastName); 55 //} 56 57 } 58 }Student 类
内容总结
以上是互联网集市为您收集整理的C# 6新语法全部内容,希望文章能够帮你解决C# 6新语法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。