BACK

Object-oriented concepts form the base of all modern programming languages. Understanding the basic concepts of object-orientation helps a developer to use various modern day programming languages, more effectively. C# (C-Sharp) is an object-oriented programming language developed by Microsoft that intends to be a simple, modern, and general-purpose programming language for application development.
The course is applicable to students who want to enter the world of object-oriented programming, using the C# language. This course provides a strong foundation in object-oriented programming approaches and the fundamentals of C# programming language.
Object orientation is a software development methodology that is based on modeling a real-world system. An object oriented program consists of classes and objects.
Let us understand the terms—class and objects
An object means a ‘material thing’ that is capable of being presented to the senses.
An object has the following characteristics:

  • It has a state
  • It may display behavior
  • It has a unique identity
    Objects interact with other objects through messages.

    Introducing C#

    A program is a set of instructions to perform a specific task. Programming languages use programs to develop software applications. A compiler is a special program that processes the statements written in a particular programming language and converts them into a machine language. This process of conversion is called compilation.
    C#, also known as C-Sharp, is a programming language introduced by Microsoft.
    C# is specially designed to work with the Microsoft’s .NET platform.
    Consider the following code example, which defines a class:
    public class Hello
    {
    public static void Main(string[] args)
    {
    System.Console.WriteLine("Hello, World! \n");
    }
    }
    ________________________________________________ public class Hello
    {
    public static void Main(string[] args)
    {
    System.Console.WriteLine("Hello, World! \n");
    }
    }
  • The class Keyword Is used to declare a class.
  • The class Name Is used as an identifier for a class.
  • The Main() Function Is the entry point of an application Is used to create objects and invoke member functions.
  • System.Console.WriteLine() Displays the enclosed text on the screen.
  • The Escape Character Displays New line character.
    Other special characters can also be displayed such as \t, \b and \r.

  • A variable is a location in the memory that has a name and contains a value.
  • A variable is associated with a data type that defines the type of data that can be stored in a variable.
    You can declare and initialize variables by using the following syntax:
    =;
    Naming variables in C# The following rules are used for naming variables in C#:
  • Must begin with a letter or an underscore.
  • Should not contain any embedded spaces or symbols Must be unique .
  • Can have any number of characters
  • Keywords cannot be used as variable names

    Compiling and Executing C# Program

    After writing the program in a Notepad, you need to compile and execute it to get the desired output. The compiler converts the source code that you write into the machine code, which the computer can understand. The following steps are needed to compile and execute a C# program.
    1. Save the code written in the Notepad with an extension .cs.
    2. To compile the code, you need to go to the Visual Studio 2005 Command Prompt window. Select Start All Programs Microsoft Visual Studio 2005 Visual Studio Tools Visual Studio 2005 Command Prompt. The Visual Studio 2005 Command Prompt window is displayed to compile the program.
    3. In the Visual Studio 2005 Command Prompt window, move to the location where the programs file is saved.
    4. Compile the program file by using the following command: csc ExecuteClass.cs
    5. To execute the code, type the following in the command prompt: ExecuteClass.exe

    The features of the object-oriented approach are:
  • Realistic modeling
  • Reusability
  • Resilience to change
  • Existence as different forms A model of a system is built in the stages of analysis, design and implementation.
    The purpose of the model is to help developers understand the reality that they are trying to imitate. In C#, a class is created by using the keyword class. It is identified by a name called the class name.
    The Console.WriteLine() method is used to display text on the screen.
    Main() is the first function which is executed in a C# program.
  • Escape characters are used to display special characters such as the newline character.
  • A variable is a named location in the memory, which contains a specific value.
  • A datatype defines the type of data that can be stored in a variable.
  • The two types of data type are Value type and Reference type.
  • The ReadLine() method is used to accept inputs from the user.
  • The using keyword is used to include the namespaces in the program.
  • A namespace contains a set of related classes. Member variables are declared inside the class body.

    Basic Data Types and their mapping to CTS (Common Type System)
    There are two kinds of data types in C#.

    Value types are passed to methods by passing an exact copy while Reference types are passed to methods by passing only their reference (handle). Implicit data types are defined in the language core by the language vendor, while explicit data types are types that are made by using or composing implicit data types.

    As we saw in the first issue, implicit data types in .Net compliant languages are mapped to types in the Common Type System (CTS) and CLS (Common Language Specification). Hence, each implicit data type in C# has its corresponding .Net type. The implicit data types in C# are:

     
    C# type   .Net type  Size in  Description
                          bytes
    bool      Boolean       1     Contains either true or false
    char      Char          2     Contains any single Unicode
                                  character enclosed in single
                                  quotation mark such as 'c'
    Integral types
    byte      Byte          1     May contain integers from 0-255
    sbyte     SByte         1     Signed byte from -128 to 127
    short     Int16         2     Ranges from -32,768 to 32,767
    ushort    UInt16        2     Unsigned, ranges from 0 to 65,535
    int       Int32         4     Ranges from -2,147,483,648 to
    (default)                     2,147,483,647
    uint      UInt32        4     Unsigned, ranges from 0 to
                                  4,294,967,295
    long      Int64         8     Ranges from
                                  -9,223,372,036,854,775,808 to
                                  9,223,372,036,854,775,807
    ulong     UInt64        8     Unsigned, ranges from 0 to
                                  18,446,744,073,709,551,615
    Floating point types
    float     Single        4     Ranges from ±1.5 × 10-45
                                  to ±3.4 × 1038 with 7 digits
                                  precision. Requires the
                                  suffix 'f' or 'F'
    double    Double        8     Ranges from ±5.0 × 10-324 to
    (default)                     ±1.7 × 10308 with 15-16 digits
                                  precision
    
    decimal   Decimal       12    Ranges from 1.0 × 10-28 to
                                  7.9 × 1028 with 28-29 digits
                                  precision. Requires the
                                  suffix 'm' or 'M'


    Implicit data types are represented in language using keywords, so each of the above is a keyword in C# (Keyword are the words defined by the language and can not be used as identifiers). It is worth noting that string is also an implicit data type in C#, so string is a keyword in C#. The last point about implicit data types is that they are value types and thus stored on the stack, while user defined types or referenced types are stored using the heap. A stack is a data structure that store items in a list in first out (LIFO) fashion. It is an area of memory supported by the processor and its size is determined at the compile time. A heap consists of memory available to the program at run time. Reference types are allocated using memory available from the heap dynamically (during the execution of program). The garbage collector searches for non-referenced data in heap during the execution of program and returns that space to Operating System.

    Examples :

    The SalesTaxCalculator class inherits the TaxCalculator and overrides its CalculateTax() method. It applies 30% tax on the price of an item (a bit harsh!) and returns the new price of the item. The SalesTaxCalculator class also defines a constructor that takes itemPrice as its parameter. If we don't provide the implementation of the CalculateTax() method in SalesTaxCalculator

     
    	class SalesTaxCalculator : TaxCalculator
    	{
    		public SalesTaxCalculator(double itemPrice)
    		{
    			this.itemPrice = itemPrice;
    		}
    	/*	public override double CalculateTax()
    		{
    			tax = 0.3 * itemPrice;
    			return itemPrice + tax;
    		}*/
    	}

     

    class SalesTaxCalculator : TaxCalculator
    	{
    		public SalesTaxCalculator(double itemPrice)
    		{
    			this.itemPrice = itemPrice;
    		}
    		public override double CalculateTax()
    		{
    			tax = 0.3 * itemPrice;
    			return itemPrice + tax;
    		}
    	}
    abstract class TaxCalculator
    	{
    		protected double itemPrice;
    		protected double tax;
    		
    		public abstract double CalculateTax();
            
    		public double Tax
    		{
    			get { return tax; }
    		}
    		public double ItemPrice
    		{
    			get { return itemPrice; }
    		}
    	}

    Dot Net Code Converter

    Select Conversion Type
    Converted Code:

    Free Web Hosting