简体   繁体   中英

Why do unboxed types have methods?

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

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

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.

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

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. Eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM