What Is C#
What Is C#
C# is pronounced "C-Sharp".
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.
The first version was released in year 2002. The latest version, C# 8, was released in September 2019.
C# is used for:
Mobile applications
Desktop applications
Web applications
Web services
Web sites
Games
VR
Database applications
And much, much more!
C# IDE
The easiest way to get started with C#, is to use an IDE.
In our tutorial, we will use Visual Studio Community, which is free to download
from https://visualstudio.microsoft.com/vs/community/.
Applications written in C# use the .NET Framework, so it makes sense to use Visual Studio, as the program, the framework, and the
language, are all created by Microsoft.
C# Install
Once the Visual Studio Installer is downloaded and installed, choose the .NET workload and click on the Modify/Install button:
After the installation is complete, click on the Launch button to get started with Visual Studio.
Enter a name for your project, and click on the Create button:
Visual Studio will automatically generate some code for your
project:
The . NET framework is a software development framework from Microsoft. It provides a controlled programming environment where software can be
developed, installed and executed on Windows-based operating systems
using System;
namespace HelloWorld
{
class Program
Console.WriteLine("Hello World!");
Example explained
Line 1: using System means that we can use classes from the System namespace.
Line 2: A blank line. C# ignores white space. However, multiple lines makes the code more readable.
Line 3: namespace is a used to organize your code, and it is a container for classes and other namespaces.
Line 4: The curly braces {} marks the beginning and the end of a block of code.
Line 5: class is a container for data and methods, which brings functionality to your program. Every line of code that runs in C#
must be inside a class. In our example, we named the class Program.
Line 7: Another thing that always appear in a C# program, is the Main method. Any code inside its curly brackets {} will be
executed. You don't have to understand the keywords before and after Main. You will get to know them bit by bit while reading this
tutorial.
Line 9: Console is a class of the System namespace, which has a WriteLine() method that is used to output/print text. In our example
it will output "Hello World!".
If you omit the using System line, you would have to write System.Console.WriteLine() to print/output text.
WriteLine or Write
The most common method to output something in C# is WriteLine(), but you can also use Write().
The difference is that WriteLine() prints the output on a new line each time, while Write() prints on the same line (note that you
should remember to add spaces when needed, for better readability):
Example
Console.WriteLine("Hello World!");
Result:
Hello World!
I will print on a new line.
Hello World! I will print on the same line.
A namespace is designed for providing a way to keep one set of names separate from another. The class names declared in one namespace does not
conflict with the same class names declared in another.
public - it is access specifier means from every where we can access it. static - access modifier means we can call this method directly using class name
without creating an object of it. void - its the return type. main - method name. string [] args - in java accept only string type of argument and stores
C# Variables
Variables are containers for storing data values.
In C#, there are different types of variables (defined with different keywords), for example:
int - stores integers (whole numbers), without decimals, such as 123 or -123
double - stores floating point numbers, with decimals, such as 19.99 or -19.99
char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
string - stores text, such as "Hello World". String values are surrounded by double quotes
bool - stores values with two states: true or false
Syntax
type variableName = value;
Where type is a C# type (such as int or string), and variableName is the name of the variable (such as x or name). The equal
sign is used to assign values to the variable.
Create a variable called name of type string and assign it the value "Ali":
Console.WriteLine(name);
Create a variable called myNum of type int and assign it the value 15:
Console.WriteLine(myNum);
You can also declare a variable without assigning the value, and assign the value later:
Example
int myNum;
myNum = 15;
Console.WriteLine(myNum);
Constant ???
A demonstration of how to declare variables of other types:
Example
int myNum = 5;
The WriteLine() method is often used to display variable values to the console window.
Example
string name = "John";
For numeric values, the + character works as a mathematical operator (notice that we use int (integer) variables here):
Example
int x = 5;
int y = 6;