C# Tutorial



C# is an elegant and type-safe object-oriented language that enables developers to build a variety of secure and robust applications that run on the .NET Framework.

You can use C# to create Windows client applications, XML Web services, distributed components, client-server applications, database applications, and much, much more.

 Important features of C#

  • Encapsulated method signatures called delegates, which enable type-safe event notifications.
  • Properties, which serve as accessors for private member variables.
  • Attributes, which provide declarative metadata about types at run time.
  • Inline XML documentation comments.
  • Language-Integrated Query (LINQ) which provides built-in query capabilities across a variety of data sources.

C# - Program Structure

Before we study basic building blocks of the C# programming language, let us look at a bare minimum C# program structure

A C# program consists of the following parts:

  • Namespace declaration
  • A class
  • Class methods
  • Class attributes
  • A Main method
  • Statements and Expressions
  • Comments

using System;
namespace HelloWorldApplication
{
   class HelloWorld
   {
      static void Main(string[] args)
      {
         
              Console.WriteLine("Hello World");
              Console.ReadKey();
      }
   }
}




Let us look at the various parts of the given program:

  • The first line of the program using System; - the using keyword is used to include the System namespace in the program. A program generally has multiple using statements.

  • The next line has the namespace declaration. A namespace is a collection of classes. The HelloWorldApplication namespace contains the class HelloWorld.

  • The next line has a class declaration, the class HelloWorld contains the data and method definitions that your program uses. Classes generally contain multiple methods. Methods define the behavior of the class. However, the HelloWorld class has only one method Main.

  • The next line defines the Main method, which is the entry point for all C# programs. The Main method states what the class does when executed.

  • The Main method specifies its behavior with the statement Console.WriteLine("Hello World");

  • WriteLine is a method of the Console class defined in the System namespace. This statement causes the message "Hello, World!" to be displayed on the screen.


  • The last line Console.ReadKey(); is for the VS.NET Users. This makes the program wait for a key press and it prevents the screen from running and closing quickly when the program is launched from Visual Studio .NET.



C# - Data Types

 Data types tell the compiler that which type of data is used for processing. 

C# provides two types of data types: 

1.Value types 

2.Reference types.


A Value type data type stores copy of the value whereas the Reference type data types stores the address of the value.

Value Types 



Data Types
Size
Values
sbyte
8 bit
-128 to 127
byte
8 bit
0 to 255
short
16 bit
-32,768 to 32,767
ushort
16 bit
0 to 65,535
int
32 bit
-2,147,483,648 to 2,147,483,647
uint
32 bit
0 to 4,294,967,295
long
64 bit
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
ulong
64 bit
0 to 18,446,744,073,709,551,615
char
16 bit
0 to 65535
float
32 bit
-1.5 x 1045 to 3.4 x 1038
double
64 bit
-5 x 10324 to 1.7 x 10308
decimal
128 bit
-1028 to 7.9 x 1028

Reference Types


Data Types
Size
Values
string
Variable length
0-2 billion Unicode characters
object
---
---


C# - Varables 



A variable is nothing but a name given to a storage area that our programs can manipulate.

Type
Example
Integral types
sbyte, byte, short, ushort, int, uint, long, ulong, and char
Floating point types
float and double
Decimal types
decimal
Boolean types
true or false values, as assigned
Nullable types
Nullable data types



int i, j, k;
char c, ch;
float f, salary;
double d;

You can initialize a variable at the time of definition as:

int i = 100;

The following example uses various types of variables:

When the above code is compiled and executed, it produces the following result:







C# -Conditional Statements

This statement allows you to branch your code depending on whether or not a certain condition is met.

IF Statement

The if statement allows you to test whether or not a specific condition is met.


Syntax

If(<Condition>) 
<statements>; 

if...else Statement

An if statement can be followed by an optional else statement, which executes when the boolean expression is false.

Syntax

if(boolean_expression)
{
   /* statement(s) will execute if the boolean expression is true */
}
else
{
   /* statement(s) will execute if the boolean expression is false */
}

The if...else if...else Statement

An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions using single if...else if statement.

Syntax

if(boolean_expression 1)
{
   /* Executes when the boolean expression 1 is true */
}
else if( boolean_expression 2)
{
   /* Executes when the boolean expression 2 is true */
}
else if( boolean_expression 3)
{
   /* Executes when the boolean expression 3 is true */
}
else
{
   /* executes when the none of the above condition is true */
}


Switch Statement

The switch statement compares two logical expressions.

Syntax

Switch(<Expression>) 
Case <Value> : 
<stmts> 
Break; 
----------------------- 
------------------------- 
------------------------ 
Default : 
<stmts> 
Break; 
}





Foreach Loop

It is specially designed for accessing the values of an array and collection.

Syntax

Foreach(type var in coll/Arr)  
{  
< statement >;  
}  


C# - Loop 



Loop Type
Description
It repeats a statement or a group of statements while a given condition is true. It tests the condition before executing the loop body.

It executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.

It is similar to a while statement, except that it tests the condition at the end of the loop body

You can use one or more loop inside any another while, for or doWhile loop.



While Loop



For Loop



Do...while Loop




C# - Arrays

An array stores a fixed-size sequential collection of elements of the same type.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.



Declaring Arrays
To declare an array in C#, you can use the following syntax:

datatype[ ] arrayName;

For example,

int [ ] array =new int[4];

string [ ] array =new string [10];

double[ ] balance;





C# - Exception Handling


An exception is a problem that arises during the execution of a program. A C# exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero.

Exceptions provide a way to transfer control from one part of a program to another. C# exception handling is built upon four keywords: try, catch, finally, and throw.

  1. Try: A try block identifies a block of code for which particular exceptions is activated. It is followed by one or more catch blocks.

  1. Catch: A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception.

  1. Finally: The finally block is used to execute a given set of statements, whether an exception is thrown or not thrown. For example, if you open a file, it must be closed whether an exception is raised or not.



  1. Throw: A program throws an exception when a problem shows up. This is done using a throw keyword.







C# Tutorial C# Tutorial Reviewed by Unknown on 05:23:00 Rating: 5

No comments: