c-sharp-example
Getting Started with c# console application
before starting, you need to download visual studio from the Microsoft website
https://visualstudio.microsoft.com/downloads/
After downloading install it. Now follow the step on how to set up or start coding in a visual studio Now open your visual studio
Hello World Example:
In C# programming language, a simple "hello world" program can be written by multiple ways. Let's see the top 4 ways to create a simple C# example:
- Simple Example
- Using System
- Using public modifier
- Using namespace
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("Welcome to kodingnotes.com!");
}
}
//Output
Welcome to kodingnotes.com!
Description
class: is a keyword which is used to define class.
Program: is the class name. A class is a blueprint or template from which objects are created. It can have data members and methods. Here, it has only Main method.
static: is a keyword which means object is not required to access static members. So it saves memory.
void: is the return type of the method. It does't return any value. In such case, return statement is not required.
Main: is the method name. It is the entry point for any C# program. Whenever we run the C# program, Main() method is invoked first before any other method. It represents start up of the program.
string[] args: is used for command line arguments in C#. While running the C# program, we can pass values. These values are known as arguments which we can use in the program.
System.Console.WriteLine("Hello World!"): Here, System is the namespace. Console is the class defined in System namespace. The WriteLine() is the static method of Console class which is used to write the text on the console.
Using namespace Default Visual Studio Template
using System;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}