Showing posts with label C# 101. Show all posts
Showing posts with label C# 101. Show all posts

Friday, April 2, 2021

C# Object Oriented Programming 101

I am trying to explain C# with Object-Oriented Programming in a much easy language with examples. To help out to clear basic thumb rules and doubts regarding the base principles.
  1. What is OOP (Object Oriented Programming)?

  2. Working with Classes and Objects

  3. Class Members

  4. Access Modifiers - Encapsulation



If you interested in more dedicated posts, you are welcome add it in comment section below and I will try to make it for you.

Happy Learning!

What is Interface?

The interface is used to achieve abstraction in C#. It can only contain method declarations and properties. It is a contract between two entities. 

I am using the same example from the previous post on Abstraction, to know more about what is abstraction you can visit the same post.

interface IBird        //Interface keyword to declare it
{
    void Sound();    //Only method signature
}

Points to remember,
  • Like abstract classes, no instance creation so no constructor is allowed.is allowed for Interfaces as well 
  • Declaring interface with I comes out of practice to identify the same. 
  • Interfaces can contain methods and properties but not fields
  • All members of interface are abstract and public, you do not need to use public access modifiers
  • Derived classes must have an implementation of Interface members, you do not require to use override keyword to implement the method like abstract classes
  • The interface provides security and standard contract 

class Dove: IBird                    //Inherited Interface
{
        public void Sound()        //interface method implementation
        {               
                Console.WriteLine("Coo Coo");
        }
 }

above is an implementation example of Interface IBird to Dove class. 


Monday, March 29, 2021

What is OOP (Object Oriented Programming)?

Object-Oriented Programming is about creating Objects out of Classes


What is a Class?

Class is nothing but a blue print or a template for an object. It contains the attributes and methods.

For example, Car is a class and Audi, Mercedes, Porche, Farrari are objects of it. 

What is an Object?

           An instantiation of the class creates and an Object. This helps to reduce the repetition of code.

For example, from above one Audi is an object created from a abstract Car class. 

Pillars of Object-Oriented Programming

  • Encapsulation

    • This means show only necessary to the outer world, achieved through Access Modifiers

  • Inheritance

    • Inheriting properties from a parent object makes it more reusable and extensible
    • Helps to remove redundant code

  • Polymorphism

    • Same method signature with various forms or taste
    • Dynamic Polymorphism
    • Static Polymorphism

  • Abstraction

    • This helps to hide the complexity in your code
    • Abstract classes and methods help to achieve multiple forms and are reusable in nature

Advantages of Object-Oriented Programming

  •  Reduce repetition of code
  •  Clear Code, Less Code
  •  Faster and manageable
  •  Easy to maintain, modify and debug
  •  Provides reusable application framework
  •  Rapid development

Do you have any questions? I would be glad to answer and help or make a dedicated post.

C# Interview Prep: 100 Common Questions for 0-3 Years Experience

Common C# Interview Questions  Basics of C# Language: What is C#? C# (pronounced C sharp) is a modern, object-oriented programming language...