为什么未装箱类型有方法?

发布于 2024-12-11 20:22:14 字数 508 浏览 0 评论 0原文

为什么你可以做类似

int i = 10;
i.ToString();
'c'.Equals('d');
1.ToString();
true.GetType();

C# 的事情?这些东西要么是原始的、字面的、未装箱的,要么是这些东西的任意组合;那么为什么他们有方法呢? 它们不是对象,因此不应该有方法。这个语法糖还有其他用途吗?如果是这样,那又怎样?例如,我可以理解拥有执行这些操作的函数:

string ToString(int number)
{
  // Do mad code
  return newString;
}

但在这种情况下,您会将其称为函数,而不是方法:

string ranch = ToString(1);

这里发生了什么?

编辑:

刚刚意识到 C# 不再是 java 的克隆,并且规则完全不同。哎呀:P

Why can you do things like

int i = 10;
i.ToString();
'c'.Equals('d');
1.ToString();
true.GetType();

in C#? Those things right there are either primitive, literal, unboxed, or any combination of those things; so why do they have methods? They are not objects and so should not have methods. Is this syntax sugar for something else? If so, what? I can understand having functions that do these things, for example:

string ToString(int number)
{
  // Do mad code
  return newString;
}

but in that case you would call it as a function, not a method:

string ranch = ToString(1);

What's going on here?

edit:

Just realised C# isn't a java clone anymore and the rules are totally different. oops :P

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

追我者格杀勿论 2024-12-18 20:22:14

他们这样做是因为规范是这么说的(而且非常好):

1.28 值类型

值类型可以是结构类型或枚举类型。 C# 提供了一组称为简单类型的预定义结构类型。
简单类型通过保留字来标识。

...

1.28.4 简单类型

C# 提供了一组称为简单类型的预定义结构类型。
简单类型是通过保留字来标识的,但是这些
保留字只是预定义结构类型的别名
系统命名空间,如下表所述。

...

因为简单类型为结构类型别名,所以每个简单类型都有
成员。例如,int 具有在 System.Int32 中声明的成员,并且
从System.Object继承的成员,以及以下语句
允许:

int i = int.MaxValue; // System.Int32.MaxValue 常量
字符串 s = i.ToString(); // System.Int32.ToString() 实例方法
字符串 t = 123.ToString(); // System.Int32.ToString() 实例方法

简单类型与其他结构类型的不同之处在于它们允许
某些附加操作:

大多数简单类型允许通过写入文字来创建值
(§1.16.4)。例如,123 是 int 类型的文字,而 'a' 是
char 类型的文字。 C# 没有提供结构体的文字
一般类型,其他结构类型的非默认值是
最终总是通过这些的实例构造函数创建
结构类型。

正如规范所解释的,简单类型具有一些超能力,例如成为 const 的能力,一种可以用来代替 new 的特殊文字语法,以及计算的能力编译时间(2+2 实际上在最终的 MSIL 流中写为 4)

但是方法(以及运算符)并不是特殊的超能力,所有结构都可以拥有它们。

该规范(对于 C# 4.0,我的复制粘贴来自早期版本)可以从 microsoft 网站下载: C# 语言规范 4.0

They act like that because the spec says so (and it's pretty nice) :

1.28 Value types

A value type is either a struct type or an enumeration type. C# provides a set of predefined struct types called the simple types.
The simple types are identified through reserved words.

...

1.28.4 Simple types

C# provides a set of predefined struct types called the simple types.
The simple types are identified through reserved words, but these
reserved words are simply aliases for predefined struct types in the
System namespace, as described in the table below.

...

Because a simple type aliases a struct type, every simple type has
members. For example, int has the members declared in System.Int32 and
the members inherited from System.Object, and the following statements
are permitted:

int i = int.MaxValue; // System.Int32.MaxValue constant
string s = i.ToString(); // System.Int32.ToString() instance method
string t = 123.ToString(); // System.Int32.ToString() instance method

The simple types differ from other struct types in that they permit
certain additional operations:

Most simple types permit values to be created by writing literals
(§1.16.4). For example, 123 is a literal of type int and 'a' is a
literal of type char. C# makes no provision for literals of struct
types in general, and nondefault values of other struct types are
ultimately always created through instance constructors of those
struct types.

As the spec explains simple types have some super powers like the ability to be const, a special literal syntax that could be used instead of new, and the capacity to be computed at compilation time (2+2 is actually written as 4 in the final MSIL stream)

But methods (as well as operators) aren't a special super powers and all structs could have them.

The specification (for C# 4.0, my copy paste is from an earlier version) could be downloaded from the microsoft website : C# Language Specification 4.0

独木成林 2024-12-18 20:22:14

Eric Lippert 最近的文章 继承和表示 对此进行了解释。(剧透:你是混淆继承和表示。)

不知道为什么你声称整数 i、字符 'c' 和整数 1 是不是物体。他们是。

Eric Lippert's recent article Inheritance and Representation explains.(Spoiler: You are confusing inheritance and representation.)

Not sure why you claim that the integer i, the character 'c' and the integer 1 are not objects. They are.

变身佩奇 2024-12-18 20:22:14

在 C# 中,所有基本类型实际上都是结构。

In C# all primitive types are actually structures.

戏蝶舞 2024-12-18 20:22:14

这样您就可以使用它们了!

能够这样做很方便,所以你也可以。

现在,为了做到这一点,可以将基元视为结构。例如,32 位整数可以作为 32 位整数处理,但也可以作为 public struct Int32 : IComparable、IFormattable、IConvertible、IComparable、IEquatable 处理。我们基本上可以两全其美。

So that you can use them!

It's convenient to be able to do so, so you can.

Now, in order to do so, primitives can be treated as structs. E.g. a 32-bit integer can be processed as a 32-bit integer, but it can also be processed as public struct Int32 : IComparable, IFormattable, IConvertible, IComparable<int>, IEquatable<int>. We mostly get the best of both worlds.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文