How to use C# Comments ???
Hello everyone, I hope you all are fine and having fun. Today's tutorial is a quick one and is about how to use C# Comments. Comment is an important part of any programming language, although its not a directly part but they indirectly help a lot especially in complex projects.
I have also shared a tutorial on How to use comments in c++, you should read that post as well because I have explained few basic concepts in it. You guys are also welcome to give us feedback about this new C# tutorial series. If you have any suggestions about it then do let us know in comments.
How to use C# Comments ???
- Comments are simple documented text lines, completely ignored by the compiler and are used to explain or provide any additional information about coding.
- There are 3 types of comments available in C#, which are:
- Single Line Comments. ( Symbol used: // )
- Multi-Line Comments. ( Initial Symbol: /* Closing Symbol */ )
- XML Documentation Comments. ( /// )
- Now, let's have have look at each one of them in detail:
Single Line Comments
- Single Line Comments are used to comment any text in a single line, we have to place two forward slashes ( // ) and everything after that will be ignored by the compiler.
- Let's have a look at How to use Single Line Comments, in below figure:
- You can see in above figure, that I have used green comments above each block to explain my code i.e. mentioning 2D & 3D arrays, accessing array elements etc.
- Moreover, I have also used comments in front of the line i.e. displaying data at location 0, 0 etc. In this case, I am just explaining a single line.
Multi-Line Comments in C#
- We can also comment out multiple lines by using /* */ these symbols.
- Let's say, you don't want to execute some part of your code i.e. 100 lines, then it would be too difficult to place // these symbols in front of each line.
- In such cases, there's a need for Multi-Line Comments, here's an example:
- At the top of above code, you can see I have used Multi-Line Comments to give a simple introduction, here you can also add description of your project/code.
- Moreover, I have also commented out Console code, I just want output using For Loop and I have used Multi-Line Comments for that.
XML Documentation Comments
- In Microsoft Visual Studio, when you hover your cursor on any object then it pops up an info box.
- If you want to add any information in this pop up box then you need to use XML Documentation Comments, as shown in below figure:
- In the above code, I have created a new class named TEPClass, we will discuss classes later in detail, but for now just focus on XML comment above that class.
- In Program class, I have called TEPClass and now when I hover my cursor over it, you can see a pop up box appeared and it has that XML comment in it.
- I have placed a red arrow in front of it, here we can place a short description of our class.
Why we need to use C# comments ???
- Let's say you have to use some old code which you have designed a year ago.
- Now when you read your own code after a year, it will be difficult to understand.
- But if you have placed some check note (comments) for each command then you can easily understand it.
That's all for C# comments. I hope you guys have understood how & why we need to use C# Comments. In next tutorial, we will have a look at How to use If Loop in C#.
How to use C# String Variables
Hello friends, I hope you all are fine and having fun with your lives. In today's tutorial, we are gonna have a look at How to use C# String Variables in your C# Projects. In the previous tutorials, we have a look at How to use C# Int Variables and also have seen How to use C# Double Variable. So, now today we are gonna see how to use C# String Variable.
We have already discussed the variables in detail so I hope that you must have the idea of what variables are and if you don't then you must read the previous tutorials first. The code is also given below in the tutorial. So, let's get started with How to use C# String Variables:
How to use C# String Variables ???
- C# also supports string variable, a string variable can save anything in it in the form of characters.
- C# String variable can be of any length.
- So, let's design a small code to show how C# String works.
- First of all design a simple C# Project and add one button and one text box in it, as shown in below figure:
- Now change the Text of your Button to Click Here and Name of the button to ClickHere.
- Similarly change the Name of the text box to txtClick.
- We have already designed a similar project in How to use C# Int variables.
- Now that our project is added so now add the below code into it an your code will look something like:
- Now you can see in the above figure, that first I have created a string variable named webBlog.
- After that I have assigned it a value, because the variable is a C# string variable that's why it can save this string easily in it.
- After that I have simply displayed it in that text box.
- You must have noticed that this time we didn't have converted the value to string as we did in C# Double variable.
- Because our variable is already a string.
- Here's the complete code for copying:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Variables
{
public partial class Form1 : Form
{
string webBlog;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void ClickHere_Click(object sender, EventArgs e)
{
webBlog = "www.TheEngineeringProjects.com";
txtClick.Text = webBlog;
}
}
}
- Now let's have a look at the results.
- So, run your project and then click on the button and the string will appear in the text box as shown in below figure:
- So that's how C# string variable works.
- Now you can save any kind of long strings in C# string variable.
- Now let's have a look at few of the string properties.
C# String Comparing
- Suppose you have two strings then How to compare them.
- So write your code as shown in below figure:
- Now you can see in the above code, I have used two C# String variables webBlog1 and webBlog2.
- I have given the same text to both of these C# string variables.
- Now after that I have used a small function called String.Compare() and it takes two C# string variables.
- After that it compares these two C# string variables and if they are same then it give 0 so that's why I have compared it to 0 means if the two C# string are equal then I have printed Both are equal otherwise not equal.
- So, that's how you can compare two C# String Variables.
- Here's the code for copy:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Variables
{
public partial class Form1 : Form
{
string webBlog1, webBlog2;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void ClickHere_Click(object sender, EventArgs e)
{
webBlog1 = "www.TheEngineeringProjects.com";
webBlog2 = "www.TheEngineeringProjects.com";
if (String.Compare(webBlog1, webBlog2) == 0)
{
txtClick.Text = "Both are equal";
}
else
{
txtClick.Text = "Both are not equal";
}
}
}
}
Find subString in C# String
- Now suppose you have a long string and you want to find some small string whether its in the long string or not.
- Then you can use another function named as contain and the below example shows how to use this command:
- In the above figure, you can see that I have initialized a C# String variable and given it a string text.
- After that I have used the function String.Contain and checked a part in the string.
- So you can see the String is actually in the main string so it will give Its present in the string otherwise not present.
- So, run your project and then click the button and you will get something as shown in below figure:
- Now, you can see it has given that its present in the string.
- Here's the code for copying:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Variables
{
public partial class Form1 : Form
{
string webBlog1, webBlog2;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void ClickHere_Click(object sender, EventArgs e)
{
webBlog1 = "www.TheEngineeringProjects.com";
if (webBlog1.Contains("Projects"))
{
txtClick.Text = "Its present in string";
}
else
{
txtClick.Text = "Its not present in string.";
}
}
}
}
Dividing a C# String
- So, now let's get something out of a string.
- Suppose in some project you have a very long C# string and you just want a part of that C# string.
- Then what you need to use is another string command which is webBlog1.Substring(4).
- So, have a look at the below code:
- Now in the above code you can see I have initialized a C# String variable and assigned it a value.
- After that, I have used second variable and then I have subString it by four characters and now run your project and click the button and you will get results as shown in below figure:
- So, you can see in the above string I have subString four characters in the initials and then the remaining is shown in the text box.
- So, that's how you can take any number of subString from the C# String.
So, that's all about C# String Variable. I hope you guys have learned something out of it. that's all for today. In the next tutorial we will learn more about C#. Till then take care and have fun !!! :)
How to use C# Double Variables
Hello friends, hope you all are fine and having fun with your lives. Today, I am going to show you guys How to use C# Double Variable in your projects. In our previous tutorial, we have seen
How to use C# Int Variable and we have also discussed about the term Variables in detail. So, I am not gonna discuss it again and gonna come straight to our topic C# Double variable.
It's my second C# tutorial in the series of C# variables. In the coming tutorials, I will discuss the remaining C# variables in detail. I have also provided the project code below but I would suggest you to do it on your own so that you get most out of it. Moreover, you should also have a look at Introduction to C# Windows Forms and How to use C# Control in Widows Forms as I am gonna use two C# Controls in today's tutorial as I did in C# Int tutorial. So, let's get started with How to use C# Double Variable:
How to use C# Double Variable ???
- C# double variable can store 64 bit value in it.
- C# double can store integer as well as decimal values means you can save decimal values like 5.2, 3.1 as well as simple integers like 2, 5, 38 etc.
- C# double variable approximate value is ±5.0 × 10^-324 to ±1.7 × 10^308 as per Microsoft Official Page and its precision is 15-16 digits.
- So, first of all, design a new simple C# project as we did in Introduction to C# Windows Forms.
- I am not gonna design a new one, instead I am gonna use the same project as I did for C# Int Variable.
- So after creating a new project, add a Button and a Text Box in it as we did in How to add C# Controls in Windows form.
- Now, I hope that you are done with the front end of your C# Project and it will look something as shown in below figure:
- Now, let's add the code behind this form, so your code will look something as shown in below figure:
- Now if you have a look at it then you will see that its exactly the same code as we did for C# Int variables.
- But there's a slight difference that now all of the three variables a, b and c are double instead of int.
- That's why I have assigned decimal values to them where a = 2.5 while b = 2.6.
- C# Int variable can't use these value because these are not integers but double can easily handle them.
- So, now their result should be 5.1 which is again a double so that's why our third variable is also double.
- Here's the code for you to copy:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Variables
{
public partial class Form1 : Form
{
double a = 2.5;
double b = 2.6;
double c;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void ClickHere_Click(object sender, EventArgs e)
{
c = a + b;
txtClick.Text = c.ToString();
}
}
}
- So, now run your project and then click the button and you will get results as shown in below figure:
- So, now you can see the value saved in C# double variable c was 5.1 which is now displayed in the Text Box.
So, that's all for today. I hope you guys have now understood How to use C# double variables in your projects. Thanks for reading. Have fun and take care. :)
How to use C# Int Variables
Hello friends, hope you all are fine and having fun with your lives. Today, I am going to share a new tutorial on C# language which is How to use C# Int variables. In our previous C# tutorials, we have designed a small project in C# which prints Hello World on the screen. Then we have seen Introduction to C# Windows Form, in which we have designed our first Windows form in C#. Then we have added the C# controls on that windows form.
Now, in today's tutorial, I am gonna introduce you to a new concept which is variables. Variables are used in every programming language and without variables we can't design even a simple code. There are different types of variables available in C# language and today among these variables we are gonna focus on C# Int variables. If you are doing engineering then you must have the idea about integers. :P So, let's get started with C# Int variables. Before discussing the C# Int variables, let's first get an overview of variables.
What are Variables ???
- In any kind of programming projects, there's a need for storing some data in temporary memory spaces. these temporary memory spaces are named as variables.
- For example, I have a simple project in which I need to add 2+2 and then I need to add 2 more in the output.
- So, in that case what I am gonna do is first of all, I am gonna do the simple addition operation and will add the 2+2 and then I need to save the output which is 4 somewhere.
- So, I am gonna save this output 4 in a variable and then I am gonna add 2 more in that variable and again I will save the output which is 6 in another variable and then I can print it on the output.
- I think now its quite clear from the above example that variables are used to store data for a short time.
- Now data can be of any type, it could be an integer or some decimal value or it could be characters.
- So, that's why we have a long list of variable types in C#, which are as follows:
- Int (Integer)
- Double
- Float
- Char
- String
- Byte
- sByte
- Decimal
- Boolean
- You can read more about the variables from Microsoft Variables Page.
We will cover all these variables in separate tutorials one by one and we will see how to use them and to work with them. So, rite now we are gonna have a look at usage of C# Int variable, so let's get started with it.
How to use C# Int variables ???
- C# Int variables are used for storing integers values. Int is actually a short form of Integer.
- So, for example, you have to save 2 in some variable then as 2 is an integer so you can save it in C# Int variable.
- But if you have 2.05 value then its not an integer because it has a decimal part so you can't save this value in an integer so C# Int variable will only save integers in it.
- Let's design a simple example on C#, as we did in Introduction to C# Windows Forms.
- Now in this project, I am gonna add a Button and a Text Box and on pressing that Button I will print a variable in this Text Box.
- So, design a simple project, as shown in below figure:
- I have changed the name of the Button to "ClickHere" and the Text of the Button to "Click Here".
- I have also changed the Name of the Text Box to "txtClick".
- Now add some code in the Button as shown in below figure:
- In the above code, you can see first of all I have declared the C# Int variables, which are a, b and c.
- Then I have also assigned a value to a and b, which is 2.
- Now in the Button Click function, I have added both the C# Int variables and then saved their result in another C# Int variable named as c.
- Finally, I have printed that c variable in a text box.
- You must be wondering why I have used toString() because text boxes can only print strings but c is an integer so that's why I have converted that integer into string.
- I will discuss this string conversion in more detail later.
- Here's the code for you to use:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Variables
{
public partial class Form1 : Form
{
int a = 2;
int b = 2;
int c;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void ClickHere_Click(object sender, EventArgs e)
{
c = a + b;
txtClick.Text = c.ToString();
}
}
}
- Now run your project and when you click the button then 4 will appear in the text box as shown in below figure:
So, I hope now you have got the idea How to use C# Int Variables. That's all for today. You should also have a look at How to use C# Double Variables. In the coming tutorial, we will discuss other C# variables just like this C# Int variable. Till then take care and have fun !!! :)
C# Tutorial
Hello everyone, I hope you all are fine and having fun with your lives. Today, I am going to share the list of all the C# tutorials, which I have posted on my blog till now. So, this post will be like a complete C# tutorial, which will have all the small and big C$ tutorials, I have posted on my blog. This list is quite small right now but I will post more C# tutorials soon and will add their links here. Actually, the C# tutorials are quite random on the blog so I am kind of compiling the list and pasting them all here so that readers won't get into much trouble finding them.
Till now, I have mostly posted the project on our blog in which I don't teach any language but provide you the code for that project. But now I have thought of writing complete series of tutorials in which I will teach different languages to beginners. I hope you will find them really helpful. So, the C# tutorial is the first tutorial in this series. I will post tutorials on other languages soon. So, let's get started with the C# tutorial:
C# Tutorial
Here are some very basic C# Tutorial, which are just for the beginners and if you know the basics of C# then I would suggest you to skip this section:
Basic Tutorials:
Note:
- You should also have a look at:
Loops in C#:
Important Concepts in C#:
C# Windows Form
Data Types in C#
Tutorials on C# Controls
Tutorials on C# Loops
Thanks for reading all those tutorials. If you still have any questions then ask in the comments and we will try our best to resolve your issues. Take care and have fun !!! :)
How to use Button in C# Windows Form ?
Hello everyone, I hope you guys are having fun with your lives. In today's tutorial, we are gonna have a look at How to use Button in C# Windows Form. In our previous tutorials, we have first discussed the Introduction to C# Windows Forms in which we have created our first C# Windows Forms Project. After that we have discussed How to add C# Controls in Windows Form, in which we have added different C# controls in that Windows form.
So, now we are gonna add back end programming in those Controls one by one. In today's tutorial, I am gonna add the code for the Buttons and you will see that it won't be any difficult to use them in your projects. So, let's get started with How to use Button in C# Windows Form.
How to use Button in C# Windows Form ???
- So, now we have two buttons in our C# Windows Form, so now First of all, let's change the text of first button from its Properties as shown in below figure:
- So, I have changed the text of Button1 to Click Here and I am gonna change the text of Button2 to Submit and they will now look like something as shown in below figure:
- Now you can see in the above figure that the Text on these buttons have changed to that which I have added in the Text section of their properties.
- Moreover, you must have also noticed that now their size has also increased, which I did by dragging the buttons.
- Now let's change the name of these buttons too so I am gonna change the Name of first button to ClickHere and that of second button to Submit.
- We can't add spaces in the Name of the Button.
- The Name Change for first button is shown in below figure:
- Now let's add the Click event for each of these buttons.
- Click event is the event which should happened when you press the button.
- So, now double click the first button and it will open up its code as shown below:
- Now you can see in the above code we have a function named ClickHere_Click, which is the click event function for first button.
- ClickHere is the name of the button which we changed above and _Click is the event. In simple words, this event will call on when the ClickHere Button is clicked.
- Next we have the arguments, which I am not currently gonna discuss here but we will have a look at them later and then we have these curly brackets { }, and that's where our code is gonna pasted for the click event of this button.
- So, add the below code into these brackets.
MessageBox.Show("Its a Click Here Button");
- Now when you pressed the Click Here Button then a sall message box will open up as shown in below figure:
- So, now you have seen, first we have added the Button then we changed its Text and Name and later we added the back end code in it so that it could perform an event on clicking.
- Now, I have added a small code in the Submit Button Click event and it looks something like this when you click it:
- So, that's all about how to use button in C# Windows Form.
That's all for today. Will meet you guys in the next tutorial on C#. Till then take care and have fun!!! :)
How to Add C# Controls in Windows Form ???
Hello everyone, hope you all are fine and having fun with your lives. Today, I am going to show you How to add C# Controls in your project. In our previous tutorial, we have seen an Introduction to C# Windows Forms in which we have created our first project and then we have discussed all about its different features like How to use Properties etc. and if you haven't read it yet then I would suggest you to read that one first as I am gonna continue from that tutorial onward.
So, I am assuming that you have read Introduction to C# Windows Forms and you have your first project created and ready to use. So, if you have dne exactly as I have shown in my previous tutorial then you have a for as shown in the below figure:
So, now we are gonna add some C# controls in this Windows form. So, let's get started with How to add C# Controls in it.
How to add C# Controls ???
- In our previous tutorial, we have discussed the Toolbox, which is available on the left side of your project.
- All the components in the toolbox are shown in the below figure:
- These are a lot of controls and we will cover most of them in our coming tutorials but rite now we are just interested in knowing How to add these C# Controls in Windows Form we created in our previous tutorial.
- So, the first C# Control I am gonna use is the Text Box, which is present in the Common Controls category.
- So, what you need to do is to drag this Text Box from toolbox to your Windows Form and then it will be placed on your Windows Form as shown in below figure:
- Now you can see in the above figure that our Form has a new C# Control in it which is a Text Box.
- Now when you select this Text Box then its Properties will open up in the Properties Section.
- From its Properties you can change many of its attributes.
- The two main attributes in Properties of any C# Controls are Name and Text.
- Name is the name of that Control with which we call it when we writing the code for that control.
- Text is the text which appears on that Control for user to view.
- Let me add another C# control on this Windows Form i.e. Button and then show you the difference between this Text and Name Property.
- So, drag a Button from the Toolbox and place it on this Windows Form as shown in below Figure:
- So, now we have two C# Controls on our Windows Form.
- Now I will select the Button and then its Properties will open up in the Properties Section and I am gonna change its Text to Click Here as shown in below figure:
- Now, lets see in the Form, what's written on the Button. :P
- Wow the magic happened :D and what we have written in the Text Section of the Properties of this Button has appeared on it.
- So, that's How the Text Property works. Whatever you written in the Text Section is appeared on that C# Control.
- The Name of Button Control is still button1 so whenever I am gonna call this button in the back end code I will use that Name.
- We are gonna do this in the next tutorial.
So, that's all for today. I hope you guys now have the idea How to add C# Controls in the Windows Form. In the coming tutorial, I am gonna add code behind each of these controls and will let you know How to use them. So, till then take care and have fun !!! :)
Introduction to C# Windows Forms
Hello everyone, hope you all are fien and having fun with your lives. Today, I am going to post a new C# tutorial in which we will have a Introduction to C# Windows Forms. Till now, we have seen Introduction to C# in which we have designed a console application in C# and after that we have discussed that project and write some code in it in First Code in C# Tutorial. So, till now we have an idea How to create a console application and how to run it easily.
I will continue with this C# console projects but first we have to discuss a little about C# Windows Forms. Microsoft Visual Studio is a very vast software so we can design different types of projects in it and also can use different languages. So, when talking about C#, we not only can design console applications but can also design C# Windows Forms in it. C# Windows Forms projects are much more easier than Console applications because in them you have a clear GUI in which you can drag and drop your components and can write the background code quite easily. So, let's get started with C# Windows Forms.
Introduction to C# Windows Forms
- First of all create a new project in Microsoft Visual Studio and in the settings select C# Windows Forms as shown in below figure:
- So, you can see in the above figure that all the steps are same as we did for creating a Console Application in Introduction to C# tutorial.
- But the only difference here is that instead of selecting Console application, we are selecting Windows forms Applications.
- Now, when you click OK then a new Project will open up as shown in below figure:
- So, now our project is ready and we can design any kind of project on it, but first let's discuss its features.
- If you have a look at it then you can see that it has three parts.
- The center part is where we have our Form1, we will change its name later.
- The on the left side we have a Toolbox which has all the components which can be used in this C# Windows Forms.
- On the right side, we have this Solution Explorer and below it we have Properties panel.
- So, let's discuss each of them separately.
Toolbox in C# Windows Forms
- Here's an image of all the components in the toolbox of C# Windows Forms:
- First of all there are categories like Common Controls etc and each of these categories has components in it which I have shown in detail by expanding these categories.
- So, these all controls can be used in C# Windows Forms and it depends on your application, which one you want to use.
- Most commonly used are buttons and text boxes etc which are in Common Controls section.
Solution Explorer in C# Windows Forms
- Here's an image of the Solution Explorer in C# Windows forms:
- This Solution Explorer shows all the files used in your C# Windows Forms Project.
- First of all, we have the Properties folder which has the AssemblyInfo.cs file, it contains the basics things about your project like name and copyrights etc.
- Then we have the References section, which has all the system library files.
- Next we have the Form1.Designer.cs file which contains out Form1 design.
- Finally we have the Program.cs file which has the back end code for our Form1.
- All the code we are gonna write for our C# Windows forms Project is gonna be add in this Program.cs file.
Properties in C# Windows Forms
- Here's an image of the Properties panel in C# Windows Forms:
- Now when you select any component on your C# Windows Forms then its properties open up.
- Rite now, I have selected the Main Form and its properties are appearing here.
- Now from these properties you can change many attributes of your project.
- Like for Form1, it says the name is Form1, so let me change the Text of it to something else as shown in below figure:
- Now you can see in the above figure that I have changed the Text of this Form1 to Introduction to C# Windows Forms.
- So, it will change the text of your Form to that as shown in below figure:
- Now you can see the Name of our Form has changed to the text which we have written in the Text section of its properties.
So, that's all about the Introduction to C# Windows form. In the coming tutorials, we are gonna add controls from the toolbox in this C# and then we will add some back end code to make them work. So, till then take care and have fun !!! :)
First Project in C#
Hello everyone, I hope you all are fine and having fun with your lives. Today, we are going to design our First Project in C#. In the previous tutorial, we have seen a detailed Introduction to C#, where we have studied its importance in the software world.
Today, we are going to create our first project in C#. So, let's get started with our first code in the C# Tutorial series.
First Project in C#
- I am going to use Microsoft Visual Studio 2019 Community Edition, which is free to use, and you can download it from its official website.
- There are different languages available in Visual Studio i.e. C, C++, C# and F#. C# is the most commonly used programming language among all.
- Now, I assume that you have installed Visual Studio and are ready to design your first project.
- So, open your Microsoft Visual Studio, and create a New Project by clicking File and then New Project, as shown in the figure on the right.
- You can also create a new project by clicking Ctrl+N.
- Now once you create the New Project, a new pop up window will open up, as shown in below figure:
- Now, as shown in the above figure, first of all, select the Visual C# as obviously we are gonna use the C# language in introduction to C#.
- Next, select the Windows and then Console Application.
- Next, we need to give a name to this Console Application, which I have given is MyFirstProject and finally click the OK button.
- Now in this window, we are gonna update our code in C#. Currently, it has simple Hello World code, as shown in below figure:
- Let's understand this code, line by line:
Namespace Declaration
- The first line of code is "using System", here the System is a built-in C# Namespace and we are declaring it at the top of our code.
- Currently, we are using just 1 namespace but later on, we are going to add a lot more and all these namespace declarations will come at the top of our code.
- You can think of a namespace as a library that has different classes & methods in it.
- So, when we declare it at the top, then all its classes become available to use in our code i.e. Console is a class of System namespace.
Projects Namespace
- Next, we have namespace TEPProject, which is the namespace of our newly created project, all of our classes will be placed inside this namespace.
- You can see this namespace has curly brackets { }, which has all the remaining code in them.
Program class
- When you run your code, the compiler finds the project's namespace and inside this namespace, it makes a search for a C# Class named Program and in Program class, it goes into the C# Method named Main.
- That's why we have the class Program inside our namespace TEPProject and inside this class, we have our static void Main(string[] args) function.
- This Main function is a static function and it has arguments with datatype string.
- This Main Function is the entry point of our compiler in our project, we have to write our code in this function or method. ( Functions are also called methods )
- Inside this Main function, we have simply printed Hello World to our Console.
- This Console is a member of namespace Systems, if we remove the namespace from the top then this Console will create the error.
So, now let's run our code and if everything goes fine then you will get something as shown in the below figure:
- We have Hello World printed in our console panel, it's doing nothing else.
- Moreover, if you are not getting things like C# Namespace or C# Methods etc. then no need to worry, we will cover all of them in coming tutorials in detail.
Extend C# Hello World Project
- Solution Explorer contains all files of your project and Program.cs is the actual code file in which I am going to write our code.
- So, let's add the below code in our Main function of Program.cs File:
Console.WriteLine("What are you learning ???");
string subject = Console.ReadLine();
Console.WriteLine("I am learning {0}", subject);
- Now your Program.cs file will look something as shown in the below figure:
- In the first line of code, I am printing "What are you learning ???" on the console.
- In the second line, I am waiting for incoming data from the console, which the user has to enter, and I am saving that data in a string variable called subject.
- In the third line, I am printing the data entered by the user on the Console.
- You must have noticed {0}, it's one way of adding data in a string and is called Place Holder Syntax.
- You can add as much data as you want, separated by commas, and can print them as {0} {1} {2} and so on.
- Let's run our code and we will get a similar output, as shown in the below figure:
- I have entered C# and it has printed it back, as you can see in the console.
- Now when you press any key, the console will stop.
- You can also use Concatenation instead of Place Holder Syntax, let's have a look at the third line, in below code:
- So, now I have used + sign instead of curly brackets {0}, it's called concatenating a string.
- If you run your code, you will get the same output, as shown in the above figure.
That's all for today. I hope you guys have got something out of it. In the next C# tutorial, we will have a look at
Introduction to Data Types in C#. So stay tuned and have fun !!! :)
Introduction to C# ( C Sharp )
Hello friends, I hope you all are fine and having fun. Today, I am going to start a new series on C# Programming Language and today we are gonna have a look at first tutorial Introduction to C#.
Are you planning to study C# programming language? If so, then you have come to the right place. Knowing how to develop computer software is perhaps the most crucial ability to master in the present era. Computers have now made their way into practically every business. Whether it's an airplane's automation or a motorcycle's speedometer, computer programming languages are everywhere around us. And out of all the computer programming languages out there, C# is the most preferred and ideal language to learn. But why? Do not worry. This article contains all the answers to your questions. So, keep reading!
C# is the most widely used programming language for designing software & websites. Most of the software, we use in our daily life are designed using this C# Language. So, let's get started with a detailed Introduction to C#.
Introduction to C#
- C# (pronounced see sharp) is a general-purpose programming language developed by Microsoft in 2000, as part of .NET framework.
- Microsoft Visual Studio is a Programming environment used for writing C#.
- C# was originally designed by Anders Hejlsberg, who currently works as the lead architect of C#.
C# Programming: The Language of God
C#, also known as the Language of God, is a contemporary, broad coding language that can help accomplish hundreds of activities and purposes in different fields. Microsoft designed C# and thus is typically used with the Windows .NET platform. The .NET Framework is actually a computer-developing framework that allows you to create applications for Windows, Azure, Web, and other programming platforms with programming languages including C#, Visual Basic, and F#.
C# can also be used in an open standard system. The latest iteration of C# is version 7.2 and is specifically for Standard Language Framework. It has a syntax that is identical to Java. So, if you are familiar with Java or C++, it would be a piece of cake for you. Do not worry if you are a beginner. This article is specifically for beginners so they can have a better understanding before actually studying the language.
History of C#
Unlike well-established languages like Java, Python, PHP, and others, C# is a modern entry to the computer programming community over twenty-one years ago. Microsoft's Anders Hejlsberg, a Denmark computer programmer with a record of renowned innovations, created this programming language in 2000. The programming language was released in 2002 and has benefited all of us till today.
Why Should You Learn C#?
Now, most people have this question: Why Should I Learn C# and Not Others? Well, C# is a one-of-a-kind programming language that's also user-friendly and quicker to comprehend. It has excellent functionality in the areas of efficiency and requires relatively minimal resources. C# may be used to develop a wide range of applications. Some of these are listed below:
- Windows consumer services by utilizing WPF, Windows Forms, and UWP
- Network applications
- Azure and Cloud applications
- Modules, features, and terminal applications
- Internet projects with ASP.NET Core and ASP.NET
- Compatibility, information, and connectivity technologies
- Android and iOS and Android phone applications by utilizing Xamarin
Other than developing applications, it is vital to learn C# because of three main reasons discussed below:
-
High Salaries
In today's world, everyone wants to make a fair living, and computer programmers are among the most fortunate people out there. During the pandemic, almost everyone was having a hard time except the computer programmers because they continued their programming. With other programming languages, you get good salary packages. However, if you know the C# programming language, you will get job opportunities in multinational companies. You can also work online because it is highly demanded nowadays.
-
C#: An Object-oriented Language
The second reason to study C# is that it is an object-intended programming language. Now, you might be thinking: what "object-intended" feature has to do anything with why you should learn C#? Well, object-intended indicates that it organizes data using objects and categories. This programming approach helps to minimize repeated coding and better regulates the data format. And all these factors help beginners to work efficiently and comfortably.
-
Developed By Microsoft
As discussed earlier, Microsoft created the C# programming language. It is a great reason why you should study C#. It means that there will be a plethora of excellent developer resources available to assist you in learning C# and no scarcity of its resources. And since Microsoft utilizes C# extensively, there is almost no chance that it will become obsolete in the coming years. So, mastering C# is, therefore, a long-term benefit.
So, you see, these are solid reasons as to why you must learn C#. Now, we understand that you want to learn even more about C#, and we got you there. The upcoming section is a more in-depth detail of C# so, start taking notes!
Features of C#
Now, let us discuss the main features of the C# programming language in this section which are:
-
Basic Features
- In the C# programming language, pointers are not available.
- It takes the characteristics of automated memory handling and garbage pickup from the .NET framework.
- The "::" and "->" operators are not used in C#.
- Direct memory modification and other risky activities are not permitted.
- "==" is utilized for the comparative procedure, and "=" is for task execution.
- Various ranges of fundamental kinds like Integers, Floats, and so on are available.
-
Modern Features
- C# follows the modern approach and is incredibly strong and convenient for creating interoperability and secure systems.
- Any element conversion into an online service that is accessible via the internet is provided in C#.
-
Type-Safe Features
- Dangerous conversions are not possible in the C# programming language.
- It is possible to check for an overflow of classes, and bound checking can be done.
- The translator immediately initializes numeric forms (objects and instances) to zeros or reference kinds to blank.
-
Object-oriented Features
- Data abstraction, inheritance, flexibility, and interfaces are all supported in C#.
- In Java language, basic kinds (float, int, double) are not things. However, C# has established structures that allow basic kinds to convert into objects.
-
Upgradeable and Scalable Features
- .NET has launched compositions that are self-defining in the areas of declaration. The manifest defines the assembly's identification, iteration, style, and electronic signature, among other characteristics.
- Upgrading software elements is a time-consuming and error-prone process. But, C# supports versioning within the language, which might affect the current programming languages. Sophisticated frameworks can also be established and changed over time.
- We erase old data and replace them with fresh ones to expand our system. Thanks to C#, there is no need to enroll in a flexible linking library.
-
Interoperability Features
- C# comes with built-in functionality for COM as well as Windows programs.
- C# lets clients modify outdated codes by using pointers as harmful code blocks.
- It enables local pointers to be used in a limited sense.
- With C#, Customers no longer need to construct the unidentified or other COM interfaces because these characteristics are already included.
- C# uses elements from Visual Basic for Applications (VB.NET) and other organized code technologies.
C# and C: What is the difference?
When studying the C# programming language, it is crucial to know the difference between C and C#. Both are coding languages, and that is why people often mess up between these two. But, do not worry. We will not let our readers go through the same mistake. Here is the difference: C is an overall-purpose coding language that includes features like recurrence, organized programming, semantic variable access, and many others. It is suitable for use in both system and integrated device programs. C is a limited modeling language whereas, C# is an object-intended programming language that includes features: garbage management, extensibility, type security, and uncomplicated class declarations. It may be used to create smartphones, desktops, and web applications. C# is a high-level programming language with a lot of complexity.
So, both programming languages have the same initials but, they are complete opposites of each other.
How C# Programing language works
There is a dedicated set of libraries that are required to run C# in a machine. This collection of libraries is called the .NET framework and it has a collection of libraries that are called CLR’s (common language runtime). The .NET library belongs to Microsoft who has written the library on top of common language infrastructure that is commonly known as CLI which is an internationally acclaimed standard because of the way it assists seamless development and easy usage.
When we write code in C#, it takes the form of IL (intermediate language) that is workable according to CLI specifications. These files then take the .dll extension after they are built. An assembly contains multiple dll files.
Basic Commands of C#
The basic commands of C# are quite similar to most popular languages such as C, C++ and Java. Moreover, C# is an object-oriented language so it has similar structures to the languages mentioned above. Some of the basic commands are listed below.
1. Using
The word
“Using” is used to include libraries and API’s into your C# code. It is similar to the famous
#include statement used in other languages. Any libraries written after the keyword Using will then be downloaded/included at runtime. It is also possible to have multiple Using statements in C#.
2. Class
The keyword class is used to define classes in C#. The syntax is as follows:
Class User{
….
}
3. Comments
Comments are a common feature of any programming language. Developers usually add commented code in their files for the understanding of other people or as a reminder to themselves if they ever revisit their own code and want to understand how things work.
Commented code is not compiled on run time. The compiler simply ignores any code written after/on a single line or any code written between a
/* and a
*/.
4. Member or Class Variable
Instances are a common occurrence in programming languages. In C# the equivalent term is member or class variable. These are variables that are usually declared and defined when the classes are declared and defined.
5. Identifier
Identifiers are used to declare classes, variables, methods and members in C#. it helps keep track of specific classes and variables that have been defined by the user. Identifiers have simple rules that are to be followed. The rules for C# are similar to the rules for other languages. Also, it is important to note that Identifier can never be a variable name in C#.
Components of C#
Component classes are traditional classes that were used in C and C++ and also made their way to modern C#. These classes are used mainly for the purpose of containment as well as cleanup. Components are present inside a container and can do various operations on the container they belong to. A lot of the business processing is done in these components.
Disadvantages of C#
We have discussed many advantages of the C# programming language in the above sections. Now it is time that you learn about some of the disadvantages of the C#. Please keep in mind that the advantages of C# outweighs its disadvantages. Some of the disadvantages are listed below:
- C# is a substandard x-platform GUI.
- Because C# is a component of the.NET platform, the program must be executed only on a Windows operating system.
- Because it is heavily reliant on the .Net platform, C# is much less versatile.
- C# provides less versatility than C++.
- Error correction demands a high level of expertise and understanding.
Conclusion
We hope that this article helped you in having a better understanding of the C# programming language. C# is an all-in-one programming language, and it has a bright future ahead of it. So, you should definitely give it a go because it is a long-term advantage. We wish you the best of luck on your journey! In the next tutorial, we will design our
First Project in C#. Till then take care and have fun !!! :)