It is an object-oriented programming language created by Microsoft that runs on the .NET Framework.
C# has roots from the C family, and the language is close to other popular languages like C++ and Java.
C# is used to develop web apps, desktop apps, mobile apps, games and much more.
We can use below code to print "Hello World" on the console/screen
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
Output : We can get output of c# program on console/desktop application/web application etc.
Comments : Comments can be used to explain C# code, and to make it more readable. It can also be used to prevent execution when testing alternative code.
// This is a comment
Console.WriteLine("Hello World!");
/* The code below will print the words Hello World
to the screen*/
Console.WriteLine("Hello World!");
Variables are containers for storing data values.
In C#, there are different types of variables like int,double,string,bool etc.
To create a variable, you must specify the type and assign it a value
However, you can add the const keyword if you don't want others (or yourself) to overwrite existing values (this will declare the variable as "constant", which means unchangeable and read-only):
string name = "Shree";
int myNum = 15;
const int myNumb = 95;
A data type specifies the size and type of variable values. It is important to use the correct data type for the corresponding variable; to avoid errors, to save time and memory, but it will also make your code more maintainable and readable. The most common data types are:
Data Type | Size | Description |
---|---|---|
int | 4 bytes | Stores whole numbers from -2,147,483,648 to 2,147,483,647 |
long | 8 bytes | Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
float | 4 bytes | Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits |
double | 8 bytes | Stores fractional numbers. Sufficient for storing 15 decimal digits |
bool | 1 bit | Stores true or false values |
char | 2 bytes | Stores a single character/letter, surrounded by single quotes |
string | 2 bytes per character | Stores a sequence of characters, surrounded by double quotes |