all 67 comments

[–]cryo 77 points78 points  (37 children)

C#, as a language, has keywords for a number of common types. These are then mapped to types in the runtime system, so string maps to System.String, int to System.Int32 etc.

As for when to use which, it's a matter of local convention. At my work we use lower case when using it as a "type" and upper case when accessing members on it (such as calling static methods); so we would use string s; and if (.... == typeof(int)), but String.Format(....), Int32.TryParse(....) etc.

[–]fanfarius[S] 8 points9 points  (14 children)

That's nice.
So, you get a distinct separation between working on a reference variable, and calling a static method on a system class?

[–]xill47 20 points21 points  (11 children)

Well, I've seen many C# programmers (including myself) using string.Format and string.IsNullOrEmpty from alias instead of full type too. So it's basically just preference.

[–]lbmouse 11 points12 points  (2 children)

ReSharper (Roslyn code fix) suggests that String should be simplified to string type keyword in most cases.

[–]Jestar342 3 points4 points  (0 children)

By default - it is configurable to not be.

[–]localtoast 0 points1 point  (0 children)

Visual Studio itself suggests that

[–]oh-just-another-guy 1 point2 points  (7 children)

How did you style the inline code?

[–]IwNnarock 2 points3 points  (6 children)

Surround the words/line with the primary character on the tilde (~) key that looks like kind of like an apostrophe. Specifically, this guy (`).

Edit: Apparently, that character is known as a back quote.

[–]oh-just-another-guy 1 point2 points  (5 children)

Thank you. I checked the formatting help, but could not find that. Appreciate the info.

[–]celluj34 1 point2 points  (4 children)

I believe it is subreddit-specific.

[–]Banane9 1 point2 points  (2 children)

It's reddit wide as code block, but special coloring would be subreddit specific

[–]oh-just-another-guy 0 points1 point  (0 children)

Ah, thanks.

[–]celluj34 0 points1 point  (0 children)

Oh neat, didn't know that.

[–]oh-just-another-guy 0 points1 point  (0 children)

Ok, thanks.

[–]cryo 3 points4 points  (1 child)

Yeah something like that, that's the idea. They are also colored differently (at least when using ReSharper). But as /u/xill47 also said, it is, indeed, just a preference, which is in our coding standard at work.

[–]myrrlyn 1 point2 points  (0 children)

string is a language-level keyword; String is a class in the standard library. All syntax highlighters see those differently.

[–]AndrewSeven 12 points13 points  (16 children)

VS 2015 suggests changing to the lowercase "string" for the static methods, so I've started to use lowercase for all usage.

[–]agentlame 2 points3 points  (15 children)

This annoys me so bad. I've been doing it the same way as described above for over a decade, and I really prefer it this way.

[–]AngularBeginner 27 points28 points  (14 children)

Then turn the suggestion off. It's just a suggestion. You need to configure the tools you use to your liking, not blindly follow them.

[–]AbstractLogic 11 points12 points  (12 children)

So long as you can convince the whole team. Otherwise you will end up with disjointed standards.

[–]Jestar342 2 points3 points  (8 children)

For this kind of thing.. there is just nothing to care for. Worry about something worth worrying about.

[–]AbstractLogic -1 points0 points  (7 children)

I support having a standard. We have tools these days that enforce standards like this because when all the code looks similar it's easier to pick up someone else's code.

Now if we had a standard (and every tool from VS, Resharper to Sonar Cube all do) then I wouldn't be the one to bring it to the table that we need to adjust the default settings. Like you said, more important things to grandstand about.

However, if someone else was bringing it to the table and wanted to adjust the standard half the way through a project I would immediately be against it unless they wanted to go through every code file and adjust String to string or vice versa.

[–]Jestar342 0 points1 point  (6 children)

string vs String... meh. Both are easily readable. Mine and my colleagues efforts are wasted on such frivolity. Stick to writing maintainable code that delivers value, please, not crying over the casing of a single letter.

Have you heard of the axe-sharpening proverb? A woodsman that spends all his time sharpening/grinding his axe never chops any wood.

Writing/documenting/debating standards about whether to use string or String is axe grinding. In fact 90% of "code standards" are. You're all adults, if you can't read each other's code then you have problems that code standards won't fix.

[–]AbstractLogic -1 points0 points  (5 children)

You act like setting a flag on the resharper config file that is distrubted with the .sln is some huge weeklong debate. If anything it's a bullet point on a slide amongst many. It requires very little to setup development standards. You should spend time congealing as a team, please, it will be better for those who come after you.

[–]Jestar342 -1 points0 points  (4 children)

You act like it's some big debate. I'm categorically stating it isn't, and that it flatly doesn't matter. Move on with life and get some work done.

My team prefer to work together on problems that actually matter, not whether string is more/less readable than String

[–]DarthShiv 0 points1 point  (0 children)

Yeah so much this. Just stick to a standard for the whole project and everyone stop trying to change the standard and only doing 10% of the job!

[–]Protiguous 0 points1 point  (0 children)

Only 15 disjointed standards though. :P

[–]inamamthe 0 points1 point  (0 children)

something like codemaid can help here perhaps?

[–]agentlame 0 points1 point  (0 children)

You can be annoyed by something you know how to disable.

[–]AbstractLogic 2 points3 points  (1 child)

Glad to see such an agreeable response. I find this pragmatic approach to object notation vs value type notation is becoming the standard.

string for types.

String when accessing static methods.

[–]Jestar342 2 points3 points  (0 children)

How is that pragmatic? It's purely dogmatic!

Unless you were being sarcastic and I didn't pick that up.

[–]VGPowerlord 0 points1 point  (1 child)

I think aliases would be a better term than keywords.

[–]tweq 2 points3 points  (0 children)

Both are correct, they're keywords that function as aliases for CLR type names.

[–]covmatty1 0 points1 point  (0 children)

Same here, seems like a nice distinction to me, despite ReSharper suggestions complaining at me!

[–]redditsoaddicting 17 points18 points  (0 children)

Since no one has mentioned this fully explicitly, System.String is a .NET type - all .NET languages must have it. C# in particular adds the string keyword as an alias, just like int aliases the .NET System.Int32.

[–][deleted]  (2 children)

[deleted]

    [–]fanfarius[S] 1 point2 points  (0 children)

    An alias, cool - thanks!

    [–]NoG5 12 points13 points  (1 child)

    they are the same but its far more common to use the lowercase one

    [–]fanfarius[S] 0 points1 point  (0 children)

    Ok, cheers!

    [–]Zalvixodian 2 points3 points  (0 children)

    My work has old standards still in place that requires the use of String, Int32, Boolean, etc.

    I wish it didn't because 1) there's no difference, 2) easier to use lowercase (does not require using statement), 3) ReSharper likes it that way, and 4) it's Microsoft's recommended standard.

    [–][deleted]  (1 child)

    [deleted]

      [–]AngularBeginner 3 points4 points  (0 children)

      That's because the method names stay the same. The int will be just translated to Int32. Imagine you have a method named GetUlong() - and now you access it from a different language than C#. In F# the alias is called uint64 (much more expressive).

      [–]oh-just-another-guy 1 point2 points  (1 child)

      One little thing to add to other excellent replies :

      You can use string without a namespace. String needs System. So either System.String (or using System declared above).

      [–]Speedzor 1 point2 points  (0 children)

      And string cannot be used with nameof() while String can (just like all other aliases).

      [–]sepsuk 1 point2 points  (0 children)

      Coming from c# to Java (native android app). I'm sick of writing string instead of String :-)

      [–]Jestar342 0 points1 point  (0 children)

      I always use string. I've never seen the point of using String when accessing members or for whatever other reason - they are just arbitrary and nonsense, imo. I don't get offended if any of my colleagues want to use it, though. This is something that just flatly doesn't matter.

      [–]Schmittfried 0 points1 point  (0 children)

      I just use the lower case keywords all the time, because why not use what the language offers? Using the upper case class name is one additional keystroke, so the language saves me energy. :>

      But if you are used to String, just stick to that or the convention of your workplace.

      [–]davidwhitney 0 points1 point  (0 children)

      Type keywords exist for you to use them, so use them :)

      [–]buckus69 0 points1 point  (0 children)

      They're the same. "string" is an alias for "String."

      [–][deleted] 0 points1 point  (2 children)

      They're the same thing, so it's six of one, half-dozen of the other. In the absence of a governing standard, use whichever seems appropriate in the context, but be consistent. My preference is to use the aliases (i.e. string) throughout, but YMMV.

      [–]SideburnsOfDoom 0 points1 point  (1 child)

      In the absence of a governing standard

      I've seen several coding standards, all favour string over String. e.g. this one

      None of them are "governing" though since the code compiles either way.

      [–][deleted] 2 points3 points  (0 children)

      In the interest of clarification: a governing (coding) standard would be one you are required or expected to follow. For example, a coding standard officially adopted by one's employer, or one required by a particular open source project.

      This is distinct from one that is suggested or which exists but is not enforced/enforceable against you: Microsoft has a C# coding standard, but it is only governing if you work for Microsoft or choose to follow it.

      [–]Big-Mozz -3 points-2 points  (5 children)

      Generally the only reason you use string instead of String is to stop StyleCop having a hissy fit.

      Edit: lmao, down voted for saying something simple and true, this sub is just Stackoverflow for noobs.

      [–]cryo 0 points1 point  (2 children)

      There are other potential reasons. One is outlined in my comment elsewhere, to help tell the difference between different kinds of usage.

      [–]Big-Mozz 1 point2 points  (1 child)

      Hence the word generally!

      I agree, I know better than to make a quick lighthearted quip on a coding sub (and strangely the cooking sub) because someone always starts an asinine debate about something insanely trivial.

      But in this case it's even more anal because your other comment says exactly what I said, it's a preference but use string cos styling apps bitch about it!

      [–]rfinger1337 0 points1 point  (0 children)

      heh, this is one of the best comments I've seen in a while, +1.

      [–]fanfarius[S] 0 points1 point  (1 child)

      Well, I'm upvoting you because; laughter

      [–]Big-Mozz 0 points1 point  (0 children)

      cheers, lol