加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 预定义类型和自定义类型之间的“对称性”是什么意思?

发布时间:2020-12-16 01:55:37 所属栏目:百科 来源:网络整理
导读:目前,我在Nut shell和Type Basics(位于第2章)子主题中阅读C#5.0,引入了预定义类型和自定义类型的对称性术语…… …为什么作者谈到这些类型之间的对称性?有什么理由说类型是对称的? 这是原始段落: A beautiful aspect of C# is that predefined types and
目前,我在Nut shell和Type Basics(位于第2章)子主题中阅读C#5.0,引入了预定义类型和自定义类型的对称性术语……

…为什么作者谈到这些类型之间的对称性?有什么理由说类型是对称的?

这是原始段落:

A beautiful aspect of C# is that predefined types and custom types have few differences. The predefined int type serves as a blueprint for integers. It holds data -32 bits- and provides function members that use that data,such as ToString. Similarly,our custom UnitConverter type acts as a blueprint for unit conversions. It holds data -the ratio- and provides function members to use that data.

解决方法

文本的含义是语言中的所有类型“表现得像对象”.例如,您可以这样做:

int i = 42;
Console.WriteLine(i.ToString()); // called a method on an int

尽可能容易地做到这一点:

DateTime d = DateTime.Now;
Console.WriteLine(d.ToString()); // called a method on a DateTime

也就是说,如果你不知道i和d的类型,你就不能通过读取调用.ToString()的代码来推断出任何东西.

将其与C语言对比:

std::string s = "hello";
std::cout << s.length();

int i = 42;
// ...but you cannot invoke any method on i; "ints are special"

这里s.length()编译的事实告诉我们s不是int,指针或任何其他原始类型.

从技术的角度来看,这种统一性不会给你带来任何好处,但它仍然是一个受欢迎的功能 – 特别是如果你只是学习语言.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读