c# – 按设计,为什么int没有单个参数ctor?
发布时间:2020-12-16 00:20:33 所属栏目:百科 来源:网络整理
导读:using System;namespace FirstApplication{ class Program { static void Main(params string[] args) { int x = new int(); x = 12; //int y = new int(12); Console.WriteLine(x); } }} 根据设计,为什么没有单个参数ctor for int,这样x可以设置为12而不必
using System; namespace FirstApplication { class Program { static void Main(params string[] args) { int x = new int(); x = 12; //int y = new int(12); Console.WriteLine(x); } } } 根据设计,为什么没有单个参数ctor for int,这样x可以设置为12而不必先设置为0? 解决方法
原因在于CLR本身. CLR不会将Int32视为任何普通的结构.它有一个特殊的类型用于存储int,它不是CLR对象.这意味着它不需要构造函数,就像C中的int没有构造函数一样.
此外,结构默认构造函数的CLR处理是将所有字段设置为其默认值.对于int,这是0.新的int()似乎是一个特例,只是0的’别名’,完全消除了任何构造函数. 另外,这只是更好. int x = 12; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |