Understanding the C# concepts through Real Time examples

Amatya
Posted by in C# category on for Beginner level | Points: 250 | Views : 30911 red flag
Rating: 4 out of 5  
 1 vote(s)

It is important for a developer to understand the concepts of OOPS or any programming language concepts through real time examples.

Introduction

It is important for a developer to understand the concepts of OOPS or any programming language concepts through real time examples.
In this article, we will look into some of the C# concepts through real time examples which you guys can easily understand and can remember for a longer time.

Descriptions

Starting in C#, the first thing which comes into picture is object and class
1) Class -  A class is a datatype that defines a collection of methods and properties in a unit. A class contains one or more objects
(I will explain object in next point, just remember object is described by the class which act as instance) which does not change during 
the execution of a program. 

Real Time Example - Class acts like a blue-print. We have a blueprint of house, through which we can make many houses which will have same properties and methods(operations). Through that blue print we can make a house of Mr. Aditya and  through that blueprint we can make a house of Mr. Amatya.
 
2) Object - Object is an instance of class. Their can more than one instance of the class, and they contain their own data.
 
 Real Time Example - Here house of Mr. Amatya and Mr. Aditya can be treated as object of class House.

Blueprint of House - Class
House of Mr. Amatya - Object

3) Array and List- Array have fixed size with continuous memory allocation of same datatype. An array index starts at zero.

Declaring array -   datatype[] arrayName;

Real Time Example - If Mr. Shryansh uses debit cards therefore  he will purchase a wallet which have 2-3 slots where he can keep his cards.
He will place his cards in continuous way so it will be easy and effective. Through this example you can remember this concept.
Now say after few year he got 4 debits cards and he is having three slots to keep his cards, now his wallet will not able to keep the cards in effective way. He will have to purchase a new wallet which should have at least 4 slots for keeping the cards.Though this scenario you can get the demerit of Array.

Whereas list is a generic type which provides most of the collections related built-in methods and properties including add, remove, search, and sort. 

Example - List<int> numbers = new List<int>();

Real Time Example - Suppose you are in general store to purchase, now don't know what are the items to buy. so by seeing the offers and requirements you can buy 10 items or even 20 items. Here List comes into picture. Your items are not fixed and you are adding the items based on your requirements, deals, and offers.After picking 20 items from the store you calculated the price and it exceeds
from 1000 (as you brought only 1000 rupees), so you want to remove some items which are not important or you want it after some days. 
So you removed 5 items and the bill was in your budget(say it becomes 900rs). In this all scenorio you can remember the concept of Lists.(some will be confused with datatypes as List have same datatypes for its items, so say here all the items which i have taken is in String dataType)

4) Abstraction - This is a concept of showing essential features and hiding non essential features from the users.

Real time Example a)- Signup for Facebook account, now a days every person who are above 18 have their Facebook account.
We just fill our details and our account is created, we don't have to know that how the application is saving our details and how its showing the details 
when we login in our respective account. So these hiding the details of saving process is termed as Abstraction in OOPS(C#)

b) Switching the fan, we don't know after switching the fan how the process goes and fan became to move.

c) Applying breaks to cars, we don't know after applying break  how the car stops.

5) Encapsulation - Encapsulation means which binds the data, operations and methods in single unit i.e. class.
 Both Abstraction & Encapsulation works hand in hand because Abstraction says what details to be made visible 
 and Encapsulation provides the level of access right to that visible details. i.e. implements the desired level of abstraction.

 Real Time Example - A car is having multiple parts like wheels,engine,steering,gears etc
 which binds together to form a object that is car. So, we can see that multiple parts of cars encapsulates itself together to form a single object.

Encapsulation = Abstraction + Data Hiding.
Encapsulation is basically used for security reason.

6) Inheritance - Inheritance is a concept of deriving a new class from the existing class.
Derived class gets all the features from base and also some new features can be added to it.

There are mainly four types of inheritance:
a) Single level inheritance
b) Multi-level inheritance
c) Hierarchical inheritance
d) Hybrid inheritance


Real time example of Single level inheritance -
In Single level inheritance, there is single base class & a single derived class 
Maruti --> Swift
Here maruti is base class and Swift is derived Class

Real time example of Multi level inheritance -
In Multilevel inheritance, there is more than one single level of derived class. 
Maruti --> Swift --> SwiftDzire
Here maruti is base class and Swift, SwiftDzire are derived Classes.

Real time example of Hierarchical inheritance -
Here multiple derived class would be extended from base class.

             |-->> Swift
     |
Maruti --
             |
             |-->> Alto
 
 
  Real time example of Hybrid inheritance - 
  Single, Multilevel, & hierarchical inheritance all together construct a hybrid inheritance.
  
     |-->> Swift -->> SwiftDzire
    |
Maruti --
             |
             |-->> Alto
 
 
7) Polymorphism -  ‘Many forms of single entity’

Real Time Example a) - We behave differently in front of parents, seniors, boss and friends. 

b) The playing ground performs multiple acts such as marriage ceremony, cricket matches, religious functions etc.

c) Akshay kumar looks differently in Sing is King, Gabbar, Airlift, Namaste London movies.
 

Hope it will help to the guys who are student or fresher in IT industry. 
That was the small effort from my aspect, if any points you guys experienced then please share.

Thanks

Page copy protected against web site content infringement by Copyscape

About the Author

Amatya
Full Name: Amatya Agyey
Member Level: Silver
Member Status: Member
Member Since: 5/9/2015 12:56:12 AM
Country: India
Feel free to share informations. mail Id ' adityagupta200@gmail.com Thanks
http://www.dotnetfunda.com
Software Enginner

Login to vote for this post.

Comments or Responses

Posted by: Amatya on: 12/21/2017 | Points: 25
Hi Music23. I didnt got you.

Login to post response

Comment using Facebook(Author doesn't get notification)