Hi Friends,
In today’s discussion, we’ll start looking C# right from the scratch. so, folks even new to C#, no need to worry, they can quickly jump into discussion and understand the concepts quickly. So, we will get started with fundamentals and gradually touch all the scenarios. So, let’s get started with 1st program which is Hello World. Here, I’ll be using VS 2013 windows project Console application.
Now, as you see in the above screen shot. This is the most basic C# program. we’ll discuss various pieces of the program bit by bit. But, for now if you simply click CNTRL+F5 button, it will produce me the below output
Now, let’s understand the program from the 1st part. 1st as you can there are several using statements which are included in the class file by default. Now, using statements are way to bring in pre-created code to your program.
So, here Visual Studio has inserted some of the most common using statements which will be used while writing the program. Now, as we progresses writing programs, we’ll be using some other using statements as well. Next comes “namespace”.
Now, a namespace is nothing but a container which keeps your codebase separated from other things declared in different namespace. So, using namespace also prevents name collision. Now, next thing which comes is class. Now, in this console app we have single class with the name “Program”.
Now, if i have to summarize class in a nutshell, classes are way to create types. We’ll understand Types in a great deal in coming sections. Within this class we have function. Functions are basically small programs or subroutines which you can call by name and execute the code.
Now, let’s go ahead and understand the fundamentals. In this 1st comes strings. String is basically a sequence of characters between double quotes. Now, let’s make it more crisp by saying anything embedded inside the double quotes (“”) is treated as string. Like in the above screenshot, we have “Hello World” between the quotes; so “Hello World” is treated as string here. Here, i can go ahead and assign the same string to a variable as well. So, here Variable is a basically a container which is going to hold the values. variables are also identified by its type like “String, Int, double etc…” or by using var as shown below in the screenshot.
now, by using var keyword, it’s compiler’s job to figure out, ok this variable is of which type. now, i can also define the above declaration as in
so, in the previous screen shot, when i hovered on the var keyword, compiler figured out it’s string. so, it basically depends on developers how they want this. Now, let’s look at some other fundamental types like “int, double”. As, the name signifies, int holds integer type values and double holds fractional values as shown below in the screen shot. Now, we can perform some operations on these values. we can add these values and the result will be implicitly converted into double. If you try to store the result in int type variable then compiler won’t allow you to store the same, it will throw casting error as shown below.
and if i run the same after correcting the above error, then it will produce me the below result.
that’s it. Now, in the next section we’ll continue this discussion with more fundamental units of C# programming and then slowly will start deep dive in C# advanced topics. Till then stay tuned and Happy Coding.
Thanks,
Rahul