Pages

C#

c# (c-Sarp)

 Object Oriented programing using c#

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

Explain features of the object-oriented methodology

            Describe the phases of the object-oriented methodology
            Define classes in C#
            Declare variables
            Write and execute C# programs

            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

 Objects:

                         An object means a ‘material thing’ that is capable of being presented to the sense

                         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.


 Characteristics of the Object-Oriented Approach

 
*: Realistic modeling
*: Reusability
*: Resilience to change
*: Existence as different forms
 
 Phases of Object Orientation
The following phases are involved in the software development:
              * The Analysis phase
              * The Design phase
              * The Implementation phase

 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.


                        Let us understand the structure of a C# program.

Consider the following code example, which defines a class:
public class Hello
{
    public static void Main(string[] args)
    {
        System.Console.WriteLine("Hello, World! \n");
    }
}
Declaring Variables :-
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:
<data_type> <variable_name>=<value>;

Data Types in C#
Represents the kind of data stored in a variable
C# provides you with various built-in data types, such as:

char
    int
    float
    double
    bool
    string
 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
     
---------------------------------------------------------------------------------------------------------------------------
 
1. Use various operators:
                    Arithmetic
                    Arithmetic Assignment

                    Unary

                    Comparison
                    Logical
2. Use conditional constructs
3. Use looping constructs
4.Applications use operators to process the data entered by a user.

Using Operators

.Operators in C# can be classified as follows:
             Arithmetic operators
             Arithmetic Assignment operators
             Unary operators
             Comparison operators
             Logical operators
Arithmetic Operators

 Arithmetic operators are the symbols that are used to perform        arithmetic operations  on variables.

  The following table describes the commonly used arithmetic operators.



Operator
Description
Example
+
Used to add two numbers
X=Y+Z;
If Y is equal to 20 and Z is equal to 2, X will have the value 22.
-
Used to subtract two numbers
X=Y-Z;
If Y is equal to 20 and Z is equal to 2, X will have the value 18.
*
Used to multiply two numbers
X=Y*Z;
If Y is equal to 20 and Z is equal to 2, X will have the value 40.
/
Used to divide one number by another
X=Y/Z;
If Y is equal to 21 and Z is equal to 2, X will have the value 10.
But, if Y is equal to 21.0 and Z is equal to 2, X will have the value 10.5.
%
Used to divide two numbers and return the remainder
X=Y%Z;
If Y is equal to 21 and Z is equal to 2, X will contain the value 1.


 Arithmetic Assignment Operators :

Arithmetic assignment operators are used to perform arithmetic operations to assign a value to an operand.
 
The following table lists the usage and describes the commonly used assignment operators.


Operator
Usage
Description
      =
                X = 5;
           Stores the value 5 in the variable X.
     +=
                X+=Y;
           Same as:
           X = X + Y;
      -=
                X-=Y;
           Same as:
          X = X - Y;
     *=
                X*=Y;
          Same as:
          X = X * Y;
     /=
                X/=Y;
         Same as:
         X = X / Y;
   %=
                 X%=Y;
        Same as:
         X = X % Y;

Unary Operators :
 

Unary operators are used to increment or decrement the value of an operand by 1.

The following table explains the usage of the increment and decrement operators.

 

Operator
Usage
Description
Example
++
++Operand;
(Preincrement operator)
Or,
Operand++; (Postincrement operator)
Used to increment the value of an operand by 1
Y = ++X;
If the initial value of X is 5, after the execution of the preceding statement, values of both X and Y will be 6.
Y = X++;
If the initial value of X is 5, after the execution of the preceding statement, value of X will be 6 and the value of Y will be 5.
--
--Operand;
(Predecrement operator)
Or,
Operand--; (Postdecrement)
Used to decrement the value of an operand by 1
Y = --X;
If the initial value of X is 5, after the execution of the preceding statement, values of X and Y will be 4.
Y = X--;
If the initial value of X is 5, after the execution of the preceding statement, value of X will be 4 and the value of Y will be 5.
 Comparison Operators :-
 Comparison operators are used to compare two values and perform an action on the  basis of the result of that comparison.
 
 The following table explains the usage of commonly used comparison operators.
Operator
Usage
Description
Example
(In the following examples, the value of X is assumed to be 20 and the value of Y is assumed to be 25)
<
expression1 < expression2
Used to check whether expression1 is less than expression2
bool Result;
Result = X < Y;
Result will have the value true.
>
expression1 > expression2
Used to check whether expression1 is greater than expression2
bool Result;
Result = X > Y;
Result will have the value false.
<=
expression1 <= expression2
Used to check whether expression1 is less than or equal to expression2
bool Result;
Result = X <= Y;
Result will have the value true.
>=
expression1 >= expression2
Used to check whether expression1 is greater than or equal to expression2
bool Result;
Result = X >= Y;
Result will have the value false.
 
Using Conditional Constructs
 Conditional constructs allow the selective execution of statements, depending on the value of expression associated with them.
The comparison operators are required for evaluating the conditions.
The various conditional constructs are:
The if…else construct
The switch…case construct
 
The if…else Construct 
 
The if…else conditional construct is followed by a logical expression where data is compared and a decision is made on the basis of the result of the comparison.
The following is the syntax of the if…else construct:
if (expression)
{
    statements;
}
     else{
               statements;
       }

 

The if…else constructs can be nested inside each other.


When if…else construct is nested together, the construct is known as cascading if…else constructs.


 The switch…case Construct


 
 













No comments:

Post a Comment