Loading, please wait...



How to create projects in c# .Net?

Aug 10, 2019 C#, C# .Net, .Net, 362 Views
you'll see how to create projects in c# .Net in this article.

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.