HI WELCOME TO KANSIRIS

Let's Discuss difference between String and string in c#

Leave a Comment
Let's Discuss difference between

                    String  and  string in c#
in one word :-

                          String is a class in dotnet class library
                          
                          string is a keyword

illustration ::

                    Here we go,consider the following program


?
1
2
3
4
5
6
7
8
9
10
//program1
//program for just declaring a string variable
using System;
class Program
{
static void Main(string[] args)
  {
  String str = "string vs String";
  }
}



above program will run without any error but doesn't have any output.

Now  i remove the namespace System from the program,the code will looks like


?
1
2
3
4
5
6
7
8
9
//program2
//program for just declaring a string variable
class Program
{
static void Main(string[] args)
  {
  String str = "string vs String";
  }
}


if i try to run the program it will shows following error message
from the above two program we understood that

  • String is class inside System Namespace
  • Since You must import System to use String
Now if i replace String with string in programas below


?
1
2
3
4
5
6
7
8
9
//program3
//program for just declaring a string variable
class Program
{
static void Main(string[] args)
{
string str = "string vs String";
}
}



program will run perfectly,So we may Conclude that
string keyword = System.String class

As always keywords always make the things more Simple.That means you can use variables of type string without using System namespace.

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.