Is String a primitive type?

后端 未结 10 1174
难免孤独
难免孤独 2020-11-27 18:36

I am curious about the string and primitive types. Article like this says string is primitive type. However second article on MSDN does not list string as primitive type.

相关标签:
10条回答
  • 2020-11-27 18:47

    they are not, because they are sequence of characters

    0 讨论(0)
  • 2020-11-27 18:54

    Change-of-stance Update: No since code doesn't lie

    Console.WriteLine(typeof(string).IsPrimitive); => False
    Console.WriteLine(typeof(int).IsPrimitive); => True
    

    -----end of update.
    But some documentation online seems to treat String as a primitive. I think Yes - based on the following definition of "primitive". (My personal definition would be a type which can't be broken down further into component types. But I guess we're just being 'pedantic' here, it's a non-issue for me mostly.)

    all primitive data types in C# are objects in the System namespace. For each data type, a short name, or alias, is provided.

    Source: http://msdn.microsoft.com/en-us/library/ms228360%28VS.80%29.aspx Another article in favor - MSDN Mag article

    Summary: I guess the answer depends on your definition of primitive, which is not unambiguously defined. Source: Eric Lippert on another SO thread.

    0 讨论(0)
  • 2020-11-27 18:58

    .NET defines (from your article):

    The primitive types are Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, Char, Double, and Single.

    So no. Inbuilt and very important: yes, but not a primitive.

    VB uses a slightly different definition to the CLI and C# it seems.

    0 讨论(0)
  • 2020-11-27 19:03

    String is a special primitive type. It is not a value type, but can be considered a primitive type because it can be created by writing literals, eg/ "hello" and it is possible to declare a constant of type string. Having said that, the value of IsPrimitive returns false

    Console.WriteLine("hello".GetType().IsPrimitive) // output = False
    

    EDIT: I want to take back my answer here. It is technically not a primitive type, but shares the properties I stated above.

    0 讨论(0)
提交回复
热议问题