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

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

概述目前,我在Nut shell和Type Basics(位于第2章)子主题中阅读C#5.0,引入了预定类型和自定义类型的对称性术语…… …为什么作者谈到这些类型之间的对称性?有什么理由说类型是对称的? 这是原始段落: A beautiful aspect of C# is that predefined types and custom types have few differences. Th 目前,我在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,指针或任何其他原始类型.

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

总结

以上是内存溢出为你收集整理的c# – 预定义类型和自定义类型之间的“对称性”是什么意思?全部内容,希望文章能够帮你解决c# – 预定义类型和自定义类型之间的“对称性”是什么意思?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1218270.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存