开发者

Why do primitive types in C# have their own operations?

A few days ago, I decided to start learning C#. So,开发者_如何学C I got a book and started reading and practicing with code. I was surprised when I saw that string in C# is considered a primitive type.

But I was more surprised when I saw that string, as well as all the other primitive types in C# have methods. I'm a Java developer and my understanding was that primitive data types don't have methods, only classes have. But in C#, the following is valid:

string name = "alex";
Console.WriteLine(name.ToUpper());

How is this possible? Are they really primitives? What am I missing here?


string is not a primitive type in C#. It's one of two predefined (i.e., part of the language specification) reference types in C# (the other being object). The primitive types in C# are Boolean (bool), Byte (byte), SByte (sbyte), Int16 (short), UInt16, Int32 (int), UInt32 (uint), Int64 (long), UInt64 (ulong), IntPtr, UIntPtr, Char (char), Double (double), and Single (single). Note that the specification states "it is also possible to use structs and operator overloading to implement new “primitive” types in the C# language" but that typeof(MyStruct).IsPrimitive is false if MyStruct is a user-defined struct.

I got a book and started reading and practicing with code. I was surprised when I saw that string in C# is considered a primitive type.

The book said this? Which book?

I'm a Java developer and my understanding was that primitive data types don't have operations, only classes have.

Plainly and simply, C# and Java are different languages. In C# there is the notion of object from which almost everything derives (yes, there are exceptions the most important of which is interfaces). From object there is a derived type called ValueType. Derivatives of ValueType are structs which have value semantics. All other derivatives of object are reference types. All of these objects encapsulate data and behavior (i.e., they can have methods).

string name = "alex";

Console.WriteLine(name.ToUpper());

How is this possible?

I don't understand your confusion with this code snippet. name is an instance of string that is definitely assigned by the string literal "alex" and we are invoking one of the overloads of the method String.ToUpper on name. Then the overload of Console.WriteLine that accepts an instance of string is invoked. You can even do this

Console.WriteLine("alex".ToUpper());

Are they really primitives?

No. string is not a primitive.

What am I missing here?

That C# and Java are related but very different programming languages.


string, in C#, is a class - it's an alias for System.String.

However, all types in .NET have methods. It is truly object oriented, and everything derives from System.Object, which also means that the methods of System.Object work on every type in C#.


Short answer is that they're not really primitives. The compiler will switch out your string with a String object.


Since C# represents all primitive data types as objects, it is possible to call an object method on a primitive data type. (Source: MSDN - Data Types - C# vs Java)


They are aliased to their matching System Namespace types in .Net. Here's a rundown of the alias list for value types in .Net:

  • bool = System.Boolean
  • byte = System.Byte
  • char = System.Char
  • decimal = System.Decimal
  • double = System.Double
  • enum = System.Enum
  • float = System.Single
  • int = System.Int32
  • long = System.Int64
  • sbyte = System.SByte
  • short = System.Int16
  • uint = System.UInt32
  • ulong = System.UInt64
  • ushort = System.UInt16
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜