C#

How to create projects in c# .Net?

How to create projects in c# .Net?

We assume you have already installed the Visual Studio software. Follow these steps in the creation  of your first C# program :

  • Open visual studio -> click File -> New -> Project and then choose Console Application as shown:


     

  • Enter a name for your project and click OK. Next, you will see some content like this.

 

Every C# console application contains a method /function named Main. Main is the starting point of every application. We will learn about the types of statements, classes, methods, arguments, and namespaces in upcoming lessons.

  • Press Ctrl+F5 to run your program. You will see the following screen as below.


     

Congratulations, you just created your first C# program. As you did not include any statements in the Main method, the program just produces a general message. Pressing any key will close the console.

To make our code print something or display some output, we need to add some statements like Console.Write or Console.WriteLine in our Main method.

For instance,

Static void Main(string[] args)
{
Console.WriteLine(“Hello World”);
}

The only difference between Console. Write and Console.WriteLine is that Console.WriteLine moves the cursor to the next line after each output.

 

To get some user input, we need to use the Console.ReadLine method to assign the input to a string variable. For instance,

Static void Main(string[] args)
{
string Name;
Console.WriteLine(“Enter your name”);
Name= Console.ReadLine();
Console.WriteLine(“Hello {0}”,Name);
}

After this code, the console window appears to look like this:

 

The Console.ReadLine method waits for user input and then assigns it to the string variable. It then displays the output.

The method Console.ReadLine() returns a string value. If you want to get the data in any other type like int, double, etc. , you need to convert it using convert functions like Convert.ToDouble, Convert.ToBoolean, Convert.ToInt32 , etc. For example,

Static void Main(string[] args)
{

int marks=Convert.ToInt32(Console.ReadLine());

Console.WriteLine(“Your marks are {0}”.marks);

}

Make sure you enter the value of respective datatype like an integer value for converting user input to an integer, etc. otherwise, the program will fail to work and cause an error.

  • Basic Syntax :

Here, I have depicted the components of a basic C# program syntax through an image.

 

Nitin Pandit

With over 10 years of vast development experience with different technologies, Nitin Pandit is Microsoft certified Most Valued Professional (Microsoft MVP) with a rich skillset that includes developing and managing IT/Web-based applications in different technologies, such as – C#.NET, ADO.NET, LINQ to SQL, WCF, and ASP.NET 2.0/3.x/4.0, WCF, WPF, MVC 5.0 (Razor), and Silverlight, along with client-side programming techniques, like jQuery and AngularJS. Nitin possesses a Master’s degree in Computer Science and has been actively contributing to the development community for its betterment. He has written more than 100 blogs/articles and 3 eBooks on different technologies to help improve the knowledge of young technology professionals. He has trained more than one lakh students and professionals, as a speaker in workshops and AppFests, conducted in more than 25 universities in North India.