Sunday, October 23, 2016

What is the difference between String and string in C#?


Example :

string s = "uday";

String S = "uday";

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

string is a c sharp keyword.

It is recommended to use string any time when you're referring to an object.

e.g :  string name = "uday";

Where as use String if you need to refer specifically to the class. e.g.

string firstname= String.Format("Uday" {0}!", name);

This is the style that Microsoft tends to use in their examples

Note: You must have to include a using System when using String, otherwise you get the following error:

The type or namespace name 'String' could not be found (are you missing a using directive or an assembly reference?)

These are the aliases defined:

Alias     CLR type

string   System.String

sbyte    System.SByte

byte     System.Byte

Note:  String represents the System.String class while the string is the keyword for the System.String that we can use to create string objects.

No comments:

Post a Comment

Thank you for visiting my blog

Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...