A1VBCode Forums

Learn Csharp Basics in 1 Hour - Csharp Tutorial For Beginners


By annaharris - 5/23/2013

Thanks for sharing such an informative post. These basic things are very important for beginners.
By annaharris - 5/29/2013

These are fundamentals of C#, if you are a beginner then you must have knowledge of all these aspects.
By How2Prog - 4/8/2013


As with all programming languages, c#.net has its own syntax, In this chapter, we'll take a look at the elements of the c#.net language: variables, constants, data type, operators, conditional statements, loops.

How To Declare variables?

Variables are used to store data in a program, The syntax for declaring a variable is:

Data_Type VariableName;

Data_Type: is the type of data that we want to store in our declared variable.

How To Declare Constants?

a constant represents permanent data that never changes, The syntax for declaring a constant is:

const int myConstant = 12;

What's Data Type?

Every variable must have a data type. A variable's data type determines the values that the variable can contain, C#.net has a wide range of data type:

byte, short, int, long, string, char, bool, float, double,decimal,... etc.

What's an operator?

An operator is a symbol that causes C# to take an action.

How to use operators?

Operators let you manipulate your data, It makes working with alphabetic characters and numeric characters easy, C#.Net comes with plenty of built-in operators:

Assignment Operators

After a variable is declared, you can assign a value to it by using an assignment statement. In C#.net, the equal sign (=) is used as the assignment operator. The syntax for assignment statements is as follows:

double pi = 3.14;

string text = "Brainy";

String Concatenation operators

The plus sign (+) allows us to concatenate strings. The syntax for string concatenation operator is as follows:

string message = "Welcome" + " To " + "How2Prog " + "Space";

Arithmetic operators:

C#.net supports various arithmetic operators for all floating-point and integer numbers. These operators are + (addition), - (subtraction), * (multiplication), / (division), ^(Exponentiation) and % (modulo).

int sum = 3 + 6;

int product = 6 * 2;

double division = 40 / 5;

double expo = Math.Pow(10, 7);

Increment and Decrement Operators

There are two more shorthand operators for incrementing and decrementing a variable by 1 is as follows:

//increments var by 1


//decrements var by 1


Comparison operators

Comparison operators compares two values and determines the relationship between them, for example:

bool result1=4>2; //result1=true because 4 is greater than 2

bool result2=5<1; //result2=false because 5 is not less then 1

bool result3 = 10!= 5; //result3=true becaus 10 and 5 are not equal

bool result4 = 3 == 6 / 2; //result4=true becaus 3 and 6/2 are equal

Logical operators

C#.Net provides the following bitwise/Logical operators:

//And (&&Wink operator: Performs an And operation:

bool result1 = (2 > 1 && 4 > 2); // true because all conditions are true

//Or (||) operator: Performs an Or operation:

bool result2 = (2 > 3 || 1 < 2); // true because there is a true condiition

if... else Statements

If statement enables you to evaluate one or many conditions and take action based on that condition. its syntax is:

if( /*Statement that can be either true or false*/)


//Do Someting


And here's an example putting if statement to work:

if (Age == 24)


Console.WriteLine("We have the same age");


The logic of the precedent Example is that it tests whether the Age is less equal to 24. If

so, it prints:

We have the same age

Suppose we wanted to execute some code for either of the two or many possible results of a condition. We can perform this task easily using an else statement along with an if statement, it's syntax is as follows:

if( /*Statement that can be either true or false*/)


//Do Someting




//Do Someting


Take a look at this example:

if (password=="123456")


Console.Write("Login succeeded");




Console.Write("Error Login");


the if statement tests whether password is equal to 123456. If so, it prints Login succeeded;

if not it prints Error Login.

The switch... case Statement

We use switch... case statement to test an expression, determine which of several cases it matches, and execute the corresponding code. Here's the syntax:

char chr = 'B';

switch (chr)


case 'A':

// Do something.


case 'B':

// Do something.


case 'C':

// Do something.


case 'D':

// Do something.



// Do something.



for the rest of this lesson, and many other csharp and vb.net tutorials, visit us at: