美文网首页
C# 10.0 属性 init

C# 10.0 属性 init

作者: Rinaloving | 来源:发表于2023-04-09 09:18 被阅读0次

    init

    • 如果属性设置为 init ,则对象只能在初始化时候赋值,此后不能再改变值。
    
    
            static void Main(string[] args)
            {
                Book book = new();
                book.Title = "好的";
                book.Name = "钩子"; // 错误
                Console.WriteLine(book.Title);
            }
    
        public class  Book
        {
            public string Name { get; init; }
            public string Title { get; set; }
        }
    
    
    QQ截图20230410091354.png

    相关文章

      网友评论

          本文标题:C# 10.0 属性 init

          本文链接:https://www.haomeiwen.com/subject/sxhbddtx.html