Tuesday 28 January 2014

C# Programming.


C# Hello World Example

A C# program basically consists of the following parts:

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

Let us look at a simple code that would print the words "Hello World":

using System;
namespace HelloWorldApplication
{
   class HelloWorld
   {
      static void Main(string[] args)
      {
         /* my first program in C# */
         Console.WriteLine("Hello World");
         Console.ReadKey();
      }
   }
}

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

Hello World

Let us look at various parts of the above 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 would contain more than one method. 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 will do when executed
  • The next line /*...*/ will be ignored by the compiler and it has been put to add additional comments in the program.
  • 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.

It's worth to note the following points:

  • C# is case sensitive.
  • All statements and expression must end with a semicolon (;).
  • The program execution starts at the Main method.
  • Unlike Java, file name could be different from the class name.

 

Encapsulation is implemented by using Access specifiers.

An Access specifier defines the scope and visibility of a class member.

C# supports the following access specifiers:

  • Public
  • Private
  • Protected
  • Internal
  • Protected internal

 

Public Access Specifier


Public access specifier allows a class to expose its member variables and member functions to other functions and objects. Any public member can be accessed from outside the class.


Private Access Specifier


Private access specifier allows a class to hide its member variables and member functions from other functions and objects. Only functions of the same class can access its private members. Even an instance of a class cannot access its private members.


Protected Access Specifier


Protected access specifier allows a child class to access the member variables and member functions of its base class. This way it helps in implementing inheritance. We will discuss this in more details in the inheritance chapter.

 

 

 

 

 

 

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.

  • TRY: A try block identifies a block of code for which particular exceptions will be activated. It's followed by one or more catch blocks.
  • 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.
  • 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.
  • THROW: A program throws an exception when a problem shows up. This is done using a throw keyword.

Syntax


Assuming a block will raise and exception, a method catches an exception using a combination of the try and catch keywords. A try/catch block is placed around the code that might generate an exception.

Code within a try/catch block is referred to as protected code, and the syntax for using try/catch looks like the following:

try
{
   // statements causing exception
}
catch( ExceptionName e1 )
{
   // error handling code
}
catch( ExceptionName e2 )
{
   // error handling code
}
catch( ExceptionName eN )
{
   // error handling code
}
finally
{
   // statements to be executed

 

No comments:

Post a Comment