Why is “string” considered a simplified version of “String”?

前端 未结 4 1822

In C# I usually use String when I\'m utilizing a method and string when declaring a variable. I read elsewhere that this is the preferred method to

相关标签:
4条回答
  • 2020-12-03 06:48

    Because it doesn't require using System; at the top.

    0 讨论(0)
  • 2020-12-03 06:51

    Because you didn't uncheck "Prefer intrinsic predefined type keyword when declaring locals, parameters and members" found under Tools > Options > Text Editor > C# > Code Style

    0 讨论(0)
  • 2020-12-03 07:02

    VS2017-2019 Tools > Options > Text Editor > C# > Code Style (>predefined type preferences:) > For member access expressions

    select "Prefer framework type"


    VS2015 Tools > Options > Text Editor > C# > Code Style

    uncheck "Prefer intrinsic predefined type keyword in member access expressions"


    Example given in VS2015-2019 for this option flips

    var local = int.MaxValue (Prefer predefined type /ticked)

    to

    var local = Int32.MaxValue (Prefer framework type /unticked)


    ReSharper - to disable it/configure the inspection severity, it is the "Replace built-in type reference with a CLR type name or a keyword" rule.

    Now nothing hints at me to change String.Format() to string.Format()

    0 讨论(0)
  • 2020-12-03 07:12

    string is an alias in C# for System.String. So technically, there is no difference. It's kinda like int vs. System.Int32.

    As far as the what you 'Should' do, string is the preferred object for variables and String for classes as it the practised choice.

    usually seen like this

    string example = "hello world";
    
    string example = String.Format("Hello World {0}!", example);
    
    0 讨论(0)
提交回复
热议问题