Вы находитесь на странице: 1из 51

INTRODUCTION TO .

NET AND OOPS

WHAT IS .NET
AND
WHY DO WE NEED .NET?
DIFFERENT FROM Procedural language
Object oriented programming

• Overcome the flaws of procedural like lack of reusability and


maintainability.
• Focusses more on data and its privacy.
• Combines all the data and functions into a single unit called object.
• Ties the data more closely to the functions that opearate on it, avoid
any modification of data from outside.
• Data of an object can be accessed only by the method associated with
that object.
What is an object?
Constructor and types of constructor
Access specifiers
Abstract class
interface
sealed class
namespaces
Properties
virtual/new/override keywords
Exception Handling
Compile and RunTime Polymorphism
DELEGATES
• Any method which has the same signature as delegate can be
assigned to delegate.
• It is very similar to the function pointer but with a difference that
delegates are a type-safe.
• There are three steps for defining and using delegates:
• 1. Declaration A delegate is declared by using the keyword delegate,
otherwise it resembles a method declaration.
• 2. Instantiation To create a delegate instance, we need to assign a
method (which has same signature as delegate) to delegate.
• 3. Invocation Invoking a delegate is like as invoking a regular method
• public delegate int MyDelagate(int a, int b);
//delegates having same signature as method 3. 4.
public class Example{
public int Sum(int a, int b)
{return a + b;
}
public int Difference(int a, int b)
{return a - b;
}
class Program
• static void Main()
•{
• Example obj = new Example();
• // 2. Instantiation
• MyDelagate multicastdel = new MyDelagate(obj.Sum);
• multicastdel += new MyDelagate(obj.Difference);
• // 3. Invocation
• multicastdel (50, 20);
•}
•}

• /* Out put

• Sum of integers is = 70
• Difference of integer is = 30

• */
• Delegates are like C++ function pointers but are type safe.
• Delegates allow methods to be passed as parameters.
• Delegates are used in event handling for defining callback methods.
• Delegates can be chained together i.e. these allow defining a set of
methods that executed as a single unit.
• Once a delegate is created, the method it is associated will never
changes because delegates are immutable in nature.
• Delegates provide a way to execute methods at run-time.
• All delegates are implicitly derived from System.MulticastDelegate,
class which is inheriting from System.Delegate class.
• Delegates are like C++ function pointers but are type safe.
• Delegates allow methods to be passed as parameters.
• Delegates are used in event handling for defining callback methods.
• Delegates can be chained together i.e. these allow defining a set of
methods that executed as a single unit.
• Once a delegate is created, the method it is associated will never
changes because delegates are immutable in nature.
• Delegates provide a way to execute methods at run-time.
• All delegates are implicitly derived from System.MulticastDelegate,
class which is inheriting from System.Delegate class.
Events
Mechanism for communication between objects, as object can notify if
something happens to it.
Loosely coupled architecture – with a single code change all the classes
need to be recompiled.
Publisher subscriber mecjanism
Have a method on the subscriber side.
Publisgher will raise an event to the subscribers – delegate is a contract
between pub and sub
And determines the signature of the event handler in the subscriber.
• generics allow you to write a class or method that can work with any
data type.
• Allows to decouple the logic from the datatype which leads in better
reusability.
• Helps to create a generic logic that can be used across all the
datatypes.
• Concept of generics being applied to collection knows as generic
collection.

• E.g. List<int>
• List<String> ------- generics
• Features of Generics
• Generics is a technique that improves your programs in many ways
such as:
• It helps you in code reuse, performance and type safety.
• You can create your own generic classes, methods, interfaces and
delegates.
• You can create generic collection classes. The .NET framework class
library contains many new generic collection classes in
System.Collections.Generic namespace.
• You can get information on the types used in generic data type at run-
time.
• Advantages of Generics
• Reusability: You can use a single generic type definition for multiple
purposes in the same code without any alterations. For example, you can
create a generic method to add two numbers. This method can be used to
add two integers as well as two floats without any modification in the code.
• Type Safety: Generic data types provide better type safety, especially in the
case of collections. When using generics you need to define the type of
objects to be passed to a collection. This helps the compiler to ensure that
only those object types that are defined in the definition can be passed to
the collection.
• Performance: Generic types provide better performance as compared to
normal system types because they reduce the need for boxing, unboxing,
and typecasting of variables or objects.
• Memory is not un-limited and we need to clean some used space in
order to make room for new objects,
• Garbage collector manages allocation and reclaiming of memory. GC
(Garbage collector) makes a trip to the heap and collects all objects
that are no longer used by the application and then makes them free
from memory.
• Destructor is a special method of a class and it is used in a class to destroy the object or
instances of classes. The destructor in c# will invoke automatically whenever the class
instances become unreachable.

• Following are the properties of destructor in c# programming language.

• In c#, destructors can be used only in classes and a class can contain only one destructor.
• The destructor in class can be represented by using tilde (~) operator
• The destructor in c# won’t accept any parameters and access modifiers.
• The destructor will invoke automatically, whenever an instance of class is no longer needed.
• The destructor automatically invoked by garbage collector whenever the class objects that are
no longer needed in application.

Вам также может понравиться