.NET

              



Introduction to Object Oriented Programming Concepts c#


Object oriented Programming system or structure is way of developing software using object. In which we can create modify and delete the object. Using oops we can store data and function in a single unit.In oops concept is implemented in our real life.

OOP  has following charactersties.

v  Object:  It is instance of class(Attribute, Behaviour, Identity)

v  Class: It is collection of Object(Method & Veriable).

v  Inheritance: It is base class derived from another class is called inheritance.(Reusability).

v  Encapsulate: It is binding of data and function into a  unit call as class and this known as encapsulate. It is provide security of data mean wrapping or grouping data in a single unit.

v  Polymorphism : It is show different behavior at different instance(different function with different parameter).  One object we can used different different method on object has different behaviour.

v Abstraction : Abstraction refers to the act of represent essential feature without include the background details or explanation. Classes use the concept of abstraction and are defined as a list of abstract attribute. It show important data and hide the unwanted data.


What is OOP?


  OOP is a design philosophy. It stands for Object Oriented Programming. Object-Oriented Programming (OOP) uses a different set of programming languages than old procedural programming languages (C, Pascal, etc.). Everything in OOP is grouped as self sustainable "objects". Hence, you gain re-usability by means of four main object-oriented programming concepts.

In order to clearly understand the object orientation, let’s take your “hand” as an example. The “hand” is a class. Your body has two objects of type hand, named left hand and right hand. Their main functions are controlled/ managed by a set of electrical signals sent through your shoulders (through an interface). So the shoulder is an interface which your body uses to interact with your hands. The hand is a well architected class. The hand is being re-used to create the left hand and the right hand by slightly changing the properties of it.


What is an Object?

 An object can be considered a "thing" that can perform a set of related activities. The set of activities that the object performs defines the object's behavior. For example, the hand can grip something or a Student (object) can give the name or address. In pure OOP terms an object is an instance of a class.


 What is a Class?


             OOP_Concepts_and_manymore/class.gif

  Class is simply a representation of a type of object. It is the blueprint/ plan/ template that describe the details of an object. A class is the blueprint from which the individual objects are created. Class is composed of three things: a name, attributes, and operations.

 public class Student

{

}

According to the sample given below we can say that the Student object, named object Student, has been created out of the Student class.

 Student objectStudent = new Student();

  In real world, you'll often find many individual objects all of the same kind. As an example, there may be thousands of other bicycles in existence, all of the same make and model. Each bicycle has built from the same blueprint. In object-oriented terms, we say that the bicycle is an instance of the class of objects known as bicycles.

  In the software world, though you may not have realized it, you have already used classes. For example, the TextBoxcontrol, you always used, is made out of the TextBox class, which defines its appearance and capabilities. Each time you drag a TextBox control, you are actually creating a new instance of the TextBox class.


How to identify and design a Class?

 This is an art; each designer uses different techniques to identify classes. However according to Object Oriented Design Principles, there are five principles that you must follow when design a class.

v  SRP - The Single Responsibility Principle : A class should have one, and only one, reason to change.

v  OCP - The Open Closed Principle : You should be able to extend a classes behavior, without modifying it.

v  LSP - The Liskov Substitution Principle : Derived classes must be substitutable for their base classes.

v  DIP - The Dependency Inversion Principle : Depend on abstractions, not on concretions.

v  ISP- The Interface Segregation Principle : Make fine grained interfaces that are client specific.



    Additionally to identify a class correctly, you need to identify the full list of leaf level functions/ operations of the system (granular level use cases of the system). Then you can proceed to group each function to form classes (classes will group same types of functions/ operations). However a well defined class must be a meaningful grouping of a set of functions and should support the re-usability while increasing expandability/ maintainability of the overall system.

 In software world the concept of dividing and conquering is always recommended, if you start analyzing a full system at the start, you will find it harder to manage. So the better approach is to identify the module of the system first and then dig deep in to each module separately to seek out classes.

   A software system may consist of many classes. But in any case, when you have many, it needs to be managed. Think of a big organization, with its work force exceeding several thousand employees (let’s take one employee as a one class). In order to manage such a work force, you need to have proper management policies in place. Same technique can be applies to manage classes of your software system as well. In order to manage the classes of a software system, and to reduce the complexity, the system designers use several techniques, which can be grouped under four main concepts named Encapsulation, Abstraction, Inheritance, and Polymorphism. These concepts are the four main gods of OOP world and in software term, they are called four main Object Oriented Programming (OOP) Concepts. 

Inheritance

Multi level Inheritance

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace oops
 {
    class Program
    {
        static void Main(string[] args)
        {
            emp_salary es = new emp_salary();
            es.e_salary();
            Console.Read();

        }
    }
   public class employee
    {
        public void emp()
        {
            string name = "suman";
            string address = "ktm";
            int phone = 98765;
            Console.WriteLine("Name:{0}", name);
            Console.WriteLine("Name:{0}", address);
            Console.WriteLine("Name:{0}", phone);
        }
    }
    public class department:employee
    {
       public void depart()
       {
           emp();
           string depart = "software";
           Console.WriteLine("depart:{0}", depart);
        }
     }
   public class emp_salary:department
   {
       public void e_salary()
       {
           depart();
           string ta = "1000";
           string da = "1500";
           string gross = "20000";
           Console.WriteLine("TA:{0}", ta);
           Console.WriteLine("DA:{0}", da);
           Console.WriteLine("Gross:{0}", gross);

       }
   }

}

Multiple Inheritance is using By Interface in c#


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace oops
 {
    class Program
    {
        static void Main(string[] args)
        {
            Employee emp = new Employee();
            Iemployee ie = (Iemployee)emp;
            ie.emp();
            Idepartment id = (Idepartment)emp;
            id.depart();
            Console.Read();
          
        }
    }
   interface Iemployee
    {
     void emp();
    }
   interface Idepartment
    {
       void depart();
   }
   public class Employee:Iemployee,Idepartment
   {
       void Iemployee.emp()
       {
           string name = "suman";
           string address = "ktm";
           int phone = 98765;
           Console.WriteLine("Name:{0}", name);
           Console.WriteLine("Name:{0}", address);
           Console.WriteLine("Name:{0}", phone);
       }
       void Idepartment.depart()
       {
           string depart = "software";
           string ta = "1000";
           string da = "1500";
           string gross = "20000";
           Console.WriteLine("depart:{0}", depart);
           Console.WriteLine("TA:{0}", ta);
           Console.WriteLine("DA:{0}", da);
           Console.WriteLine("Gross:{0}", gross);

       }
    }
}



Polymorphism

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace oops
{
    class Polymorphism
    {
        static void Main(string[] args)
        {
            Console.WriteLine("addition:{0}",calculate(10,10));
            Console.WriteLine("addition:{0}", calculate(10,10,11));
            Console.Read();
        }
     
            static int calculate(int a, int b)
            {
                return a + b;
            }
            static  int calculate(int a, int b, int c)
            {
                return a + b + c;
            }
        }
    }

   
Encapsulation & Abstraction

Two concepts that go together in the object oriented approach are Encapsulation & Abstraction. Abstraction is the representation of only the essential features of an object, while Encapsulation is the hiding of the non-essential features.

Think of a person driving a car. He does not need to know the internal working of the engine or the way gear changes work, to be able to drive the car (Encapsulation). Instead, he needs to know things such as how much turning the steering wheel needs, etc (Abstraction).

Consider the example from the programmer’s perspective who wants to allow the user to add items to a list. The user only needs to click a button to add an item (Abstraction). The mechanism of how the item is added to the list is not essential for him (Encapsulation).

Types of Access Specifiers


  • public - The members (Functions & Variables) declared as public can be accessed from anywhere.
  • private - Private members cannot be accessed from outside the class. This is the default access specifier for a member, i.e if you do not specify an access specifier for a member (variable or function), it will be private. Therefore, string PhoneNumber; is equivalent to private string PhoneNumber;
  • protected - Protected members can be accessed only from the child classes.
  • internal - Internal members can be accessed from anywhere within the application. This is the default access specifier for a class.



Abstract Class:

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace Sample_Project
{
    class Program
    {
       public static void Main()
        {
            subclass1 sc1 = new subclass1();
            Test(sc1);
            subclass2 sc2 = new subclass2();
            Test(sc2);
            Console.Read();
        }
       public static void Test(AbstractClass ac)
        {
            ac.outPut("test");
        }
    }
    public abstract class AbstractClass
    {
        abstract public void outPut(string sOutPutString);
    }
    public class subclass1 : AbstractClass
    {
    public override void outPut(string sSource)
        {
            string s =Convert.ToString(sSource.ToUpper());
            Console.WriteLine("Call to subClass1.Output() from within{0}:::",s);

        }
    }
   public class subclass2 : AbstractClass
        {
        public override void outPut(string sSource)
            {
                string s = Convert.ToString(sSource.ToLower());
                Console.WriteLine("Call to subClass1.Output() from within{0}:::",s);

            }
        }

 }




 

Encapsulate


using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace Sample_Project
{
    class Employee
    {
        private string empName;
        private int empID;
        private float curPay;
        private int empAge;
        private string empSSN;

        public string Name
        {
            get { return empName; }
            set { empName = value; }
        }

        public int ID
        {
            get { return empID; }
            set { empID = value; }
        }

        public float Pay
        {
            get { return curPay; }
            set { curPay = value; }
        }

        public int Age
        {
            get { return empAge; }
            set { empAge = value; }
        }

        public string SocialSecurityNumber
        {
            get { return empSSN; }
            set { empSSN = value; }
        }

     



        public Employee() { }

        public Employee(string name, int id, int age, float pay, string ssn)
        {
            empName = name;
            empID = id;
            empAge = age;
            curPay = pay;
            empSSN = ssn;
        }

        public void GiveBonus(float amount)
        {
            curPay += amount;
        }

        public void DisplayStats()
        {
            Console.WriteLine("Name: {0}", empName);
            Console.WriteLine("ID: {0}", empID);
            Console.WriteLine("Age: {0}", empAge);
            Console.WriteLine("SSN: {0}", empSSN);
            Console.WriteLine("Pay: {0}", curPay);
        }

        static void Main(string[] args)
        {
            Employee e = new Employee("Ben", 987, 30, 140000, "231988946");
            e.GiveBonus(20000);
            e.DisplayStats();
            e.Name = "Ken";
            Console.WriteLine("Employee is named: {0}", e.Name);
            Console.ReadLine();

          
        
        }
       
    }
}


.NET Framework

v  .Net Framework
v  Assmbly (Difference between other assembly and .net assembly
v  Dllvs EXE
v  Manifest
v  Namespace vs Assemble
v  Compilation of .net Code , IL Code (Half compiled Code)
v  JIT
v  NGEN
v  .Net Architecture
v  CAS
v  Dll Hell
v  Versioning
v  String Names, Weakly typed references and strongly typed references
v  Private vs Shared Assembly
v  GacUtil
v  Delay signing
v  Value types, Reference Types, Stack, Heap
v  Ref vs. Out
v  Boxing, Unboxing and performance
v  GC,GC Cycle
v  Managed vs. Unmanaged objects
v  Constructor and Destructors
v  Finalize dispose pattern


C# Code Optimization Tips

   Code optimization is the vital factor for any applications. Its importance is high. As a good professional programmer we should consider about that. This article describes how can we optimize a program? orcode optimization techniques. Summary of the article:

      v  What is Code Optimization?

v  Optimization of C# Code


What is Code Optimization?
   In computing technology, optimization is the process of modifying a system to improve its performance or efficiency. The system can be a single computer program, a collection of computers or internet. Good code optimization can makes the code to run faster and use fewer hardware resources. Even it can make easier to debug and maintain.

In computer programming, code optimization is done by transforming the code to improve its performance (like code execution time, code size, minimum resources utilization). This transformation can be made either at a high level or at a low level.
The main point of code optimization is to improve the code. There are two parts:

ü  Make the program faster (in terms of execution times or steps)

ü  Make the program smaller (in terms of memory)


Optimization of C# Code

    Code optimization is an important aspect of writing an efficient C# program. The following tips will helps to increase the speed and efficiency of C# code and application:

v  We should follow standard naming convention

v  The variables and methods/functions name should be relevant and small as far as possible

v  Use X++ instead of X=X+1. Both returns the same result but X++ use fewer characters

v  Remove codes and variables whose are not used

v  If possible remove white spaces (superfluous spaces, new lines, tabs, and comments etc). It increases the code sizes

v  Use List<> instead of ArrayList. Because List<> is less cost effective and use ‘for’ Loop instead of ‘for-each’ Loop




IT Industrial Training .NET Framework

C#, ASP.NET with Window Form Application, Silverlight, WPF, ASP.NET MVC, ASP.NET CORE,Web Service, Web API, Jquery, Bootstraps, Ajax, Javascript , SAP Crystal Report, MS SQL Server, Oracle and Live Projects.

No comments:

Post a Comment