Sunday, 8 January 2017

Object Oriented Programming ( c# )



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. Everything in OOP is grouped as self sustainable "objects". Hence, you gain re-usability by means of four main object-oriented programming concepts.Below are object oriented programming concepts
C# provides full support for object-oriented programming including encapsulation, inheritance, and polymorphism.
Encapsulation means that a group of related properties, methods, and other members are treated as a single unit or object.
Inheritance describes the ability to create new classes based on an existing class.
Polymorphism means that you can have multiple classes that can be used interchangeably, even though each class implements the same properties or methods in different ways.

Now we will discuss about oops concepts in c#

Classes and Objects

classes describe the type of objects, while objects are usable instances of classes. So, the act of creating an object is called instantiation. Using the blueprint analogy, a class is a blueprint, and an object is a building made from that blueprint. 



To define a Class in C#:
Class ClassName
{
// Methods, properties, fields, events, delegates  
    // and nested classes go here. 
}

Class Members

Each class can have different class members that include properties that describe class data, methods that define class behavior, and events that provide communication between different classes and objects.

Properties and Fields:
Fields and properties represent information that an object contains. Fields are like variables because they can be read or set directly.
To define a field:
Class SampleClass 
    public string sampleField; 
Most properties have methods or procedures to both set and get the property value. However, you can create read-only or write-only properties to restrict them from being modified or read.
In C#, you can omit the get or set property method. However, auto-implemented properties cannot be
read-only or write-only.

To define a method of a class:           
class SampleClass 
    public int Method(string Param) 
    { 
        // Insert code here 
    } 

Constructors
Constructors are class methods that are executed automatically when an object of a given type is created. Constructors usually initialize the data members of the new object. A constructor can run only once when a class is created. Furthermore, the code in the constructor always runs before any other code in a class. However, you can create multiple constructor overloads in the same way as for any other method.

To define a constructor for a class:
public class SampleClass 
    public SampleClass() 
    { 
        // Add code here 
    } 

Destructors
Destructors are used to destruct instances of classes. In the .NET Framework, the garbage collector
automatically manages the allocation and release of memory for the managed objects in your
application. However, you may still need destructors to clean up any unmanaged resources that your
application creates. There can be only one destructor for a class.

Instantiating Classes
To create an object, you need to instantiate a class, or create a class instance.          
SampleClass sampleObject = new SampleClass(); 
After instantiating a class, you can assign values to the instance's properties and fields
and invoke class methods.  

// Set a property value. 
sampleObject.sampleField = "Sample String"
// Call a method. 
sampleObject.Method();

Inheritance
Inheritance enables you to create a new class that reuses, extends, and modifies the behavior that is
defined in another class. The class whose members are inherited is called the base class, and the class
 that inherits those members is called the derived class. However, all classes in C# implicitly inherit
from the Object class that supports .NET class hierarchy and provides low-level services to all classes.
Note:
C# doesn't support multiple inheritance, i.e. you can specify only one base class for a derived class.

To inherit from a base class:
class DerivedClass:BaseClass{} 
By default all classes can be inherited. However, you can specify whether a class must not be used as
a base class, or create a class that can be used as a base class only.

To specify that a class cannot be used as a base class:
public sealed class A { }  
To specify that a class can be used as a base class only and cannot be instantiated:
public abstract class B { } 

Overriding Members
By default, a derived class inherits all members from its base class. If you want to change the behavior of the inherited member, you need to override it. That is, you can define a new implementation of the method, property or event in the derived class.

The following modifiers are used to control how properties and methods are overridden:
C# Modifier
Definition
Allows a class member to be overridden in a derived class.
Overrides a virtual (overridable) member defined in the base class.
Requires that a class member to be overridden in the derived class.
Hides a member inherited from a base class

Interface
Interfaces, like classes, define a set of properties, methods, and events. But unlike classes, interfaces do not provide implementation. They are implemented by classes, and defined as separate entities from classes. An interface represents a contract, in that a class that implements an interface must implement every aspect of that interface exactly as it is defined.

To define an interface:                
interface ISampleInterface 
    void doSomething(); 
}      

To implement an interface in a class:           
class SampleClass : ISampleInterface 
    void ISampleInterface.doSomething() 
    { 
        // Method implementation. 
    } 

Generics
Classes, structures, interfaces and methods in the .NET Framework can include type parameters that
define types of objects that they can store or use. The most common example of generics is a collection, where you can specify the type of objects to be stored in a collection.

To define a generic class: 
Public class SampleGeneric<T>  
    public T Field; 
}  

To create an instance of a generic class:
SampleGeneric<
string> sampleObject = new SampleGeneric<string>(); 
sampleObject.Field = "Sample string"

Delegates
delegate is a type that defines a method signature, and can provide a reference to any method with a compatible signature. You can invoke (or call) the method through the delegate. Delegates are used to pass methods as arguments to other methods.

Note:
Event handlers are nothing more than methods that are invoked through delegates. For more information about using delegates in event handling

To create a delegate:
public delegate void SampleDelegate(string str);
To create a reference to a method that matches the signature specified by the delegate:         
class SampleClass 
    // Method that matches the SampleDelegate signature. 
    public static void sampleMethod(string message) 
    { 
        // Add code here. 
    } 
    // Method that instantiates the delegate. 
    void SampleDelegate() 
    { 
        SampleDelegate sd = sampleMethod; 
        sd("Sample string"); 
    } 
}

Source:
https://msdn.microsoft.com/en-us/library/mt656686.aspx

No comments:

Post a Comment