0% found this document useful (0 votes)
252 views

What Is C#

C# is an object-oriented programming language created by Microsoft that runs on the .NET Framework. It has roots in the C programming language but is simpler than C++. C# can be used to create a variety of applications including mobile, desktop, web, and more. The most common way to write C# code is using the Visual Studio IDE from Microsoft.

Uploaded by

taha zafar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
252 views

What Is C#

C# is an object-oriented programming language created by Microsoft that runs on the .NET Framework. It has roots in the C programming language but is simpler than C++. C# can be used to create a variety of applications including mobile, desktop, web, and more. The most common way to write C# code is using the Visual Studio IDE from Microsoft.

Uploaded by

taha zafar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

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!

Why Use C#?


 It is one of the most popular programming language in the world
 It is easy to learn and simple to use
 It has a huge community support
 C# is an object oriented language which gives a clear structure to programs and allows code to be reused, lowering
development costs.
 As C# is close to C, C++ and Java, it makes it easy for programmers to switch to C# or vice versa

C# IDE
The easiest way to get started with C#, is to use an IDE.

An IDE (Integrated Development Environment) is used to edit and compile code.

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.

On the start window, choose Create a new project:


Then click on the "Install more tools and features" button:
Choose "Console App (.NET Core)" from the list and click on the Next button:

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

static void Main(string[] args)

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.

Note: Every C# statement ends with a semicolon ;.

Note: C# is case-sensitive: "MyClass" and "myclass" has different meaning.

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!");

Console.WriteLine("I will print on a new line.");

Console.Write("Hello World! ");

Console.Write("I will print on the same line.");

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

Declaring (Creating) Variables


To create a variable, you must specify the type and assign it a value:

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":

string name = "Ali";

Console.WriteLine(name);

Create a variable called myNum of type int and assign it the value 15:

int myNum = 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;

double myDoubleNum = 5.99D;

char myLetter = 'D';

bool myBool = true;

string myText = "Hello";

The WriteLine() method is often used to display variable values to the console window.

To combine both text and a variable, use the + character:

Example
string name = "John";

Console.WriteLine("Hello " + name);

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;

Console.WriteLine(x + y); // Print the value of x + y

You might also like