0% found this document useful (0 votes)
18 views7 pages

C# Tutorial

Uploaded by

opra.lagrosa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
18 views7 pages

C# Tutorial

Uploaded by

opra.lagrosa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 7

I.

C# Introduction

What is C#?

o C# is pronounced “C-Sharp”.
o It is an Object-oriented programming language created by Microsoft that runs on the .NET Framework.
o C# has roots from the C family, and the language is close to other popular languages like C++ and
Java.
o The first version was released in year 2002. The latest version, C# 12, was released in November
2023.

C# is used for:

 Mobile applications
 Web applications
 Desktop applications
 Web services
 Websites
 Games
 VR
 Database applications

Why use C#?

 It is one of the most popular programming languages in the world.


 It is easy to learn and simple to use.
 It has 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.

II. C# Get Started

C# IDE

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


o An IDE (Integrated Development Environment) is used to edit and compile code.
o In our tutorial, we will use Visual Studio Community, which is free to download from
https://visualstudio.microsoft.com/vs/community/.
o 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.
o Continue here for installation: https://www.w3schools.com/cs/cs_getstarted.php.

Program.cs:

Output: Hello World!

2
Don’t worry if you don’t understand the code above – we will discuss it in detail in later chapters. For now,
focus on how to run the code.

Run the program by pressing F5 button on your keyboard (or click on “Debug” -> “Start Debugging”).
This will compile and execute your code.

III. C# Syntax

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 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.

Don’t worry if you don’t understand how using System, namespace, and class works. Just think of it as
something that almost always appears in your program, and that you will learn more about them in a later
chapter.

Line 7: Another thing that always appears 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 a case-sensitive; “MyClass” and “myclass” have different meaning.

Note: Unlike Java, the name of the C# file does not have to match the class name, but they often do (for
better organization). When saving the file, save it using a proper name and add “.cs” to the end of the
filename. To run the example above on your computer, make sure that C# is properly installed: Go to the
Get Started Chapter for how to install C#.

IV. C# Output

To output values or print text in C#, you can use the WriteLine() method:

Example:

You can add as many WriteLine() methods as you want. Note that it will add new line for each
method:

Example:

2
You can also output numbers, and perform mathematical calculations:

Example:

The Write Method

There is also a Write() method, which is similar to WriteLine().

The only difference is that it does not insert a new line at the end of the output:

Example:

Note that we add an extra space when needed (after “Hello World!” in the example above), for better
readability.

Output: Hello World! I will print on the same line.

In this tutorial, we will only use WriteLine() as it makes it easier to read the output of the code.

V. C# 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.

C# Single-line Comments

Single-line comments start with two forward slashes (//).

Any text between // and the end of the line is ignored by C# (will not be executed).

This example uses a single-line comment before a line of code:

Example:

This example uses a single-line comment at the end of a line of code:

Example:

2
C# Multi-line Comments

Multi-line comments start with /* and ends with */.

Any text between /* and */ will be ignored by C#.

This example uses a multi-line comment (a comment block) to explain the code:

Example:

VI. C# Variables (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

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.
To create a variable that should store text, look at the following example:

Example:

Create a variable name called name of type string and assign it the value “John”:

To create a variable that should store a number, look at the following example:

Example:

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

You can also declare a variable without assigning the value, and assign the value later:

Example:

2
Note that if you assign a new value to an existing variable, it will overwrite the previous value:

Example:

Change the value of myNum to 20:

Other Types

A demonstration of how to declare Variables of other types:

Example:

You will learn more about data types in a later chapter.

C# Variables (Constants)

If you don't want others (or yourself) to overwrite existing values, you can add the const keyword
in front of the variable type.
This will declare the variable as "constant", which means unchangeable and read-only:
Example:

The const keyword is useful when you want a variable to always store the same value, so that others (or
yourself) won’t mess up your code. An example that is often referred to as a constant is PI (3.14159…)

Note: You cannot declare a constant variable without assigning the value. If you do, an error will occur:
A const field requires a value to be provided.

C# Variables (Display Variables)

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:

2
You can also use the + character to add a variable to another variable:

Example:

For numeric values, the + character works as a mathematical operator (notice that we use int (integer)
variables here):

Example:

From the example above, you can expect:

 x stores the value 5


 y stores the value 6
 Then we use the WriteLine() method to display the value of x + y, which is 11

C# Variables (Multiple Variables)

Declare Many Variables

To declare more than one variable of the same type, use a comma-separated list:

Example:

You can also assign the same value to multiple variables in one line:

Example:

C# Variables (Identifiers)

2
All C# variables must be identified with unique names.

These unique names are called identifiers.

Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume).

Note: It is recommended to use descriptive names to create understandable and maintainable code:

Example:

The general rules for naming variables are:

 Names can contain letters, digits and the underscore character (_)
 Names must begin with a letter or underscore.
 Names should start with a lowercase letter and cannot contain whitespace.
 Names are case-sensitive ("myVar" and "myvar" are different variables)
 Reserved words (like C# keywords, such as int or double) cannot be used as names.

VII. C# Data Types

You might also like