标签:Console string C# Name WriteLine 6.0 CSharp6.0 public name
namespace CSharp6._0 { public class EmployeeInfo { /// <summary> /// 只读属性只能通过构造函数赋值 /// </summary> /// <param name="name"></param> /// <param name="age"></param> public EmployeeInfo(string name, int age) { Name = name; Age = age; } public string Name { get; } public int Age { get; } public override string ToString() => $"{Name}+{Age}"; } }
using System; namespace CSharp6._0 { public class StaticClass { public static void TestMethod() { Console.WriteLine("using static"); } public static string TestException(string name) { try { throw new Exception(name); } catch (Exception ex) when (ex.Message.Contains("张三")) { return "异常筛查"; } } } }
using System; using System.Collections.Generic; using static CSharp6._0.StaticClass; namespace CSharp6._0 { class Program { static void Main(string[] args) { //只读自动属性 EmployeeInfo employeeInfo = new EmployeeInfo("张三", 29); Console.WriteLine(employeeInfo.Name); //using static StaticClass.TestMethod();//以前的调用方式 TestMethod(); //Null条件与算符 EmployeeInfo employeeInfo1 = null; string name = employeeInfo1?.Name;//当类不为null时读取Name属性 Console.WriteLine(name); employeeInfo1 = new EmployeeInfo(null, 29); name = employeeInfo1?.Name ?? "测试";//当类的属性为null时,赋值“测试” Console.WriteLine(name); //字符串内插 string str1 = "张三"; string str2 = $"{{{str1}}}属于内插字符串"; Console.WriteLine(str2); //异常筛选器 string str = TestException("张三"); Console.WriteLine(str); //nameof表达式 string className = nameof(StaticClass); Console.WriteLine(className);//StaticClass //使用索引器初始化关联集合 Dictionary<int, string> pairs = new Dictionary<int, string> { {200, "成功" }, {500, "系统错误" } }; Console.WriteLine(pairs[200]); } } }
标签:Console,string,C#,Name,WriteLine,6.0,CSharp6.0,public,name 来源: https://www.cnblogs.com/gygtech/p/14295678.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。