C#
What is .Net?
.NET Framework (pronounced dot net) is a software framework developed by Microsoft that runs primarily on Microsoft Windows. .Net framework is a managed application development platform which help programmers to create platform independent applications. It includes a large class library known as Framework Class Library (FCL) and provides language interoperability (each language can use code written in other languages) across several programming languages known as Common Language Runtime (CLR). Its mean .Net Frame work support all object oriented languages introduce by Microsoft like C++, Vb.Net and C# dot FSharp etc.
Framework Example:
But, I would like to share one example which I only thought of today. If I told you to cut a piece of paper with dimensions 5m by 5m then surely you would do that. But then I ask you to cut 1000 pieces of paper of the same dimensions. Then you won't do the measuring 1000 times, obviously you would make a frame of 5m by 5m and then with the help of it you would be able to cut 1000 papers in less time. So, what you did is made a framework which would do that type of task. So, instead of performing the same type of task again and again for the same type of applications, what you do is create a framework having all those facilities together in one nice packet, hence providing the abstraction for your application and more importantly many applications.
OR
A framework is a set of common and prefabricated software building blocks that programmers can use, extend or customize for specific computing solutions. With frameworks developers do not have to start from scratch each time they write an application. Frameworks are built from collection of objects so both the design and code of the framework may be reused. - JavaFramework.
OR
A skeleton of an application into which developers plug in their code and provides most of the common functionality. -- E. Gamma, et al., "Design Patterns", Addison-Wesley, 1995
What is FCL and CLR?
FCL:
The Framework Class Library (FCL) is a standard library defined in the Common Language Infrastructure (CLI) .The FCL is a collection of reusable classes, interfaces and value types.
- · System.CodeDom,
- · System.Collections,
- · System.Diagnostics,
- · System.Globalization,
- · System.IO,
- · System.Resources
- · System.Text
CLR:
Short for Common Language Runtime, a runtime environment that manages the execution of .NET program code and provides services such as memory and exception management, debugging and profiling, and security. The CLR is a major component of the .NET framework.
CLR also is known as the Virtual Execution System (VES).
CLI:
The Common Language Infrastructure (CLI) is an open specification developed by Microsoft. These specification standardized by ISO and ECMA that describes executable code and a runtime environment. This allow multiple high-level languages to be used on different computer platforms without being rewritten for specific architectures.
What is C#?
C# is designed to be a platform-independent language in the tradition of Java (although it is implemented primarily on Windows). Its syntax is similar to C and C++ syntax, and C# is designed to be an object-oriented language. There are, for the most part, minor variations in syntax between C++ and C#. Main has no return type, there are no semicolons after class names, there are some (to C++ programmers) strange decisions regarding capitalization - such as the capitalization of Main. Other a few differences, the syntax is often the same. This decision is reasonable, in light of the fact that C syntax has been used with several other languages - notably Java.
Similar to Java, C# does not support multiple inheritance; instead it provides Java's solution: interfaces. Interfaces implemented by a class specify certain functions that the class is guaranteed to implement. Interfaces avoid the messy dangers of multiple inheritance while maintaining the ability to let several classes implement the same set of methods.
Another helpful feature of C# is garbage collection. Therefore, it is unnecessary to include a destructor for each class unless a class handles unmanaged resources; if so, it's necessary to release control those resources from within the class (The Finalize function is used to clear up these unmanaged resources; it can even be abbreviated with the same syntax as a C++ destructor). Of course, C# also provides direct access to memory through C++ style pointers, but these pointers are not garbage collected until specifically released by the programmer.
What is an assembly?
It is unit of deployment. It is a file generated on successful compilation of .Net application. It can be either a DLL or an EXE.
What are the features of Assembly?
They are self-Describing. They consists of metadata which tells what are the methods, properties etc. present in the assembly.
- · Assembly can be loaded side-by side thus achieving side by side execution.
- · Installation of an assembly is easier.
- · Assemblies solve the DLL HELL problem.
What are Value Types? Give example
Variables that store data are called value types. Value types are stored on stack.these veriable can be null.
They contain the actual values. Eg - int, enum, structs.
What are Reference Types? Give Example.
Variables that store reference to actual data are called Reference types. Reference types
Stored on heap but contain the address on heap. Can contain null values.
E.g. - class, interface, delegate, string, object, Array
What is boxing and unboxing?
Boxing:-
Converting a value type to a reference type .This is implicit during boxing follow 3 things happen-
· Memory is allocated on heap,
· Value is copied from stack to heap,
· Reference is updated to point to heap
Example:-
int i = 123;
object o = i;
here if you make i= 456;
then o=123, //value does not change
Unboxing:-
Converting reference type to value type. The value which is being unboxed has to be boxed first.
Example:-
int i = 123; // a value type
object o = i; // boxing
int j = (int)o; // unboxing
What do you mean by Pass By Value ?
Actual value is copied to another variable
Example:-
static void Main()
{
int a=4;
int b=5;
AddValue(a, b);
Console.WriteLine("Pass By Value ");
Console.WriteLine(a + " " + b);
Console.ReadLine();
}
//Actual Method Pass by Value
private static void AddValue(int a,int b)
{
a=6;
b=7;
}
Note: Even though the value of a and b is changed in the actualfunction it does not chane in Main Function.
What do you mean by Pass By Reference?
The calling functions passes the address of the variable instead of the actual values .Any
Changes made inside the actual method will be reflected in the calling function.
Example:-
static void Main()
{
int a=4;
int b=5;
Add(ref a, ref b);
Console.WriteLine("Pass By Refference ");
Console.WriteLine(a+ " " + b);
Console.ReadLine();
}
//Actual Method Pass by Refference
private static void Add(ref int a,ref int b)
{
a=6;
b=7;
}
Note: The value a and b will be change in Main function a=6, b=7
C# Pass by Value Example
class Program
{
static void Square(int a, int b)
{
a = a * a;
b = b * b;
Console.WriteLine(a +" "+b);
}
static void Main(string[] args)
{
int num1 = 5;
int num2 = 10;
Console.WriteLine(num1 +" "+num2);
Square(num1, num2);
Console.WriteLine(num1 + " " + num2);
Console.ReadLine();
}
}
Output
Before:
5 10
In Function:
25 10
After Function:
5 10
C# Pass by Reference Example
class Person
{
public int age;
}
class Program
{
static void Square(Person a, Person b)
{
a.age = a.age * a.age;
b.age = b.age * b.age;
Console.WriteLine(a.age+" "+b.age);
}
static void Main(string[] args)
{
Person p1 = new Person();
Person p2 = new Person();
p1.age = 5;
p2.age = 10;
Console.WriteLine(p1.age +" "+p2.age);
Square(p1, p2);
Console.WriteLine(p1.age + " " + p2.age);
Console.ReadLine();
}
}
Before:
5 10
Function Call:
25 100
After Function Call:
25 100
Templates:
Templates are a feature of the C++ programming language that allows functions and classes to operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one.
Templates are of great utility to programmers in C++, especially when combined with multiple inheritance and operator overloading. The C++ Standard Library provides many useful functions within a framework of connected templates.
Friend function
In object-oriented programming, a friend function that is a "friend" of a given class is
allowed access to private and protected data in that class that it would
not normally be able to as if the data was public. [1] Normally, a function
that is defined outside of a class cannot access such information.
Declaring a function a friend of a class allows this, in languages
where the concept is supported.
using namespace std;
class Distance
{
private:
int meter;
public:
Distance(): meter(0){ }
friend int func(Distance); //friend function
};
int func(Distance d) //function definition
{
d.meter=5; //accessing private data from non-member function
return d.meter;
}
int main()
{
Distance D;
cout<<"Distace: "<<func(D);
return 0;
}
Output
Distance: 5
What is encapsulation?
Encapsulation is defined 'as the process of enclosing one or more items within a physical or logical package'. Encapsulation, in object oriented programming methodology, prevents access to implementation details.
Relationship in OOP:
Relationship defines the connection between objects. This explains how objects are connected to each other’s and how they will behave.
Association:
It represents a relationship between two or more objects where all objects have their own lifecycle and there is no owner. The name of an association specifies the nature of relationship between objects. This is represented by a solid line.
Let’s take an example of relationship between Teacher and Student. Multiple students can associate with a single teacher and a single student can associate with multiple teachers. But there is no ownership between the objects and both have their own lifecycle. Both can be created and deleted independently.
Aggregation:
It is a specialized form of Association where all object have their own lifecycle but there is ownership. This represents “whole-part or a-part-of” relationship. This is represented by a hollow diamond followed by a line.
Let’s take an example of relationship between Department and Teacher. A Teacher may belongs to multiple departments. Hence Teacher is a part of multiple departments. But if we delete a Department, Teacher Object will not destroy.
Composition:
It is a specialized form of Aggregation. It is a strong type of Aggregation. In this relationship child objects does not have their lifecycle without Parent object. If a parent object is deleted, all its child objects will also be deleted. This represents “death” relationship. This is represented by a solid diamond followed by a line.
Let’s take an example of relationship between House and rooms. House can contain multiple rooms there is no independent life of room and any room cannot belongs to two different house if we delete the house room will automatically delete.
Let’s take another example of relationship between Questions and options. Single questions can have multiple options and option cannot belong to multiple questions. If we delete questions options will be automatically deleted.
Dependency:
It represents a relationship between two or more objects where an object is dependent on another object(s) for its specification or implementation. This is represented by a dashed arrow.
Let’s take an example of relationship between client and service. A client is dependent on the service for implementing its functionalities.
Let’s take another example of relationship between a client and a supplier. A client is dependent on the supplier for supplying products. If the supplier will not supply the products, client cannot use those products.
Another Description about Relationship:
Extracting real world relationships from a requirement
The whole point of OOP is that your code replicates real world objects, thus making your code readable and maintainable. When we say real world, the real world has relationships. Let’s consider the simple requirement listed below:
1. Manager is an employee of XYZ limited corporation.
2. Manager uses a swipe card to enter XYZ premises.
3. Manager has workers who work under him.
4. Manager has the responsibility of ensuring that the project is successful.
5. Manager's salary will be judged based on project success.
If you flesh out the above five point requirement, we can easily visualize four relationships:-
1. Inheritance
2. Aggregation
3. Association
4. Composition
Let’s understand them one by one.
Requirement 1: The IS A relationship
If you look at the first requirement (Manager is an employee of XYZ limited corporation), it’s a parent child relationship or inheritance relationship. The sentence above specifies that Manager is a type of employee, in other words we will have two classes: parent class Employee, and a child class Manager which will inherit from the Employee class.
Note: The scope of this article is only limited to aggregation, association, and composition. We will not discuss inheritance in this article as it is pretty straightforward and I am sure you can get 1000s of articles on the net which will help you in understanding it.
Requirement 2: The Using relationship: Association
Requirement 2 is an interesting requirement (Manager uses a swipe card to enter XYZ premises). In this requirement, the manager object and the swipe card object use each other but they have their own object life time. In other words, they can exist without each other. The most important point in this relationship is that there is no single owner.
The above diagram shows how the SwipeCard class uses the Manager class and the Manager class uses the SwipeCard class. You can also see how we can create objects of the Manager class and SwipeCard class independently and they can have their own object life time.
This relationship is called the “Association” relationship
The above diagram shows how the SwipeCard class uses the Manager class and the Manager class uses the SwipeCard class. You can also see how we can create objects of the Manager class and SwipeCard class independently and they can have their own object life time.
This relationship is called the “Association” relationship.
Requirement 3: The Using relationship with Parent: Aggregation
The third requirement from our list (Manager has workers who work under him) denotes the same type of relationship like association but with a difference that one of them is an owner. So as per the requirement, the Manager object will own Worker objects.
The child Worker objects can not belong to any other object. For instance, a Worker object cannot belong to a SwipeCard object.
But… the Worker object can have its own life time which is completely disconnected from the Manager object. Looking from a different perspective, it means that if the Manager object is deleted, the Worker object does not die.
Requirements 4 and 5: The Death relationship: Composition
The last two requirements are actually logically one. If you read closely, the requirements are as follows:
· Manager has the responsibility of ensuring that the project is successful.
· Manager's salary will be judged based on project success.
Below is the conclusion from analyzing the above requirements:
Manager and the Project objects are dependent on each other.
The lifetimes of both the objects are the same. In other words, the project will not be successful if the manager is not good, and the manager will not get good increments if the project has issues.
Below is how the class formation will look like. You can also see that when I go to create the project object, it needs the manager object.
This relationship is termed as the composition relationship. In this relationship, both objects are heavily dependent on each other. In other words, if one goes for garbage collection the other also has to be garbage collected, or putting from a different perspective, the lifetime of the objects are the same. That’s why I have put in the heading “Death” relationship.
Putting things together
Below is a visual representation of how the relationships have emerged from the requirements.
Summarizing:
To avoid confusion henceforth for these three terms, I have put forward a table below which will help us compare them from three angles: owner, lifetime, and child object.
Association
|
Aggregation
|
Composition
| |
Owner
|
No owner
|
Single owner
|
Single owner
|
Life time
|
Have their own lifetime
|
Have their own lifetime
|
Owner's life time
|
Child object
|
Child objects all are independent
|
Child objects belong to a single parent
|
Child objects belong to a single parent
|












Comments
Post a Comment