Yes, the title is a bit confusing if you are very new in the .NET community. If you have enough experience in C# then you might be thinking how the CLS works and the concept behind the screen, bla ..bla.
This article is only for new developers. If you go for a .NET interview then the interviewer might ask one question. "What is the difference between string and String in C# .NET". Please observe the case of "string", the first string is uncapitalized and and the other is capitalized. The interviewer may ask this question to check your theoretical knowledge.
In my old college days, one of my friends (who reads less and asks more, Ha ..Ha..) asked me this question. Still I can remember, I took a few hours to give him an answer and show him practically, exactly what happens behind the scenes. Ok, in this article we will see exactly what the difference is (if any) beween them, not only string but there are many like that.
For example, the difference between int and Int32 and so on.
How many languages you have used in the .NET platform?
If you are a young developer (like me) then the answer is 1 (only C#) and our respected senior people will say 2 (C# and VB). Oh, you have written more than that? You are good among good developers. Let's return to our explanation. Microsoft is demanding that the .NET framework support more than 32 programming languages to develop applications. So, we can use nearly all high-lavel languages, including Java, PHP, C++, Python and many more.
Now, the question is, are the same data types available in all programming languages? The answer is no. In PHP there is no concept of int, char and so on . Everything is represented by var. Whereas in C++ , C# supports int, char, float and many more pre-defined data types.
Ok, so hold onto the concept taht the .NET framework supports multiple languages to develop programs where each language does not have a similar data type or the same data type.
How does the .NET Framework process them?
Here is the main understanding. All data types are converted into a .NET data type. The question is, how? If we remind ourselves of the basic concept of the code execution process, we will see that when we compile any program in the .NET environment it will produce IL code rather than native code. Here is the execution chart.
Then the JIT compiler takes that IL code and generates corresponding native code. In the above image we see how the code is transfered to the .NET environment.
So, the concept is that whatever the data type and programming language is, we may use it to develop an application but at the day's end it is converted into .NET 's own data type.
The point
Now we will solve our question with a sample example. The difference between the capital 'S' in "String" and the small 's' in "string". Have a look at the following example.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
sing System.Diagnostics;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
string name1 = "sourav kayal";
String name2 = "sourav kayal";
Console.ReadLine();
}
}
}
The example is very simple, we have defined only two string variables but one string starts with a capital 's' and the other with a small 's'. We have compiled the code and built it. If we then open it in ILDASM then we will find the following code. Here we are seeing that all have been converted simply to "string".
Ok, so the concept is "String is a data type of C# whereas string is a data type of .NET and in IL code everything is converted into a .NET data type.
What about int and Int32 ,Int64 ?
Those conversions are also very similar with string conversion. In the following example we will see how they convert one data type into another data type. Try to understand the following code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
int a = 10;
Int16 b= 10;
Int32 c = 10;
Int64 d = 10;
Console.ReadLine();
}
}
}
Again, there is nothing in the code above. We have only declared a few variables. At first we declared int a=10 and in IL the output we are seeing that it has changed into Int32.
Try to understand the basic concept. All data types are converted into a .NET data type. So, here "int" is a data type of C# and Int32 is a data type of .NET, so that int was converted to Int32.
In the case of bool?
I hope, you have guessed that. Yes, it is the same for Boolean. In the following code we have defined bool and Bool.
class Program
{
static void Main(string[] args)
{
bool a = true;
Boolean b = false;
Console.ReadLine();
}
}
Here is our IL code. As expected, we see that Boolean has been converted into bool.
Conclusion
In this article we saw how a data type is converted into the .NET environment. Hope you have understood the concept. Keep learning. Happy day.