--------------------------------------------------------------------------------------------------------------------------
Prepared By : Uday Shah (HOD - IT)
E-Mail : rupareleducation@gmail.com
Contact No : 7600044051
:: C# Unit 2 ::
1.
Method
with ref and out parameters
·
ref and out both are used when we want to return more
then one value from the function.
·
They both indicate the parameter is pass by reference.
·
Out modifier should be assign before passed.
·
But there is no restriction with ref modifier.
·
Ref means that value is already set and method can
read it and can modify it.
·
But out means that value is not set and method must
set it before return and could not read before setting value.
Example
of ref.
·
According to above example the value of I is an
integer and we pass it to the function with the help of ref parameter. It means
value is already set method can read and modify it.
Example
of Out parameter
·
According to above example the value of I is an
integer and we pass to an function with the help of out parameter it means
value should be assign before modify it.
2.
Operator
Overloading.
·
Overloading operator are function with special name.
·
The keyword operator follow by the symbol for the
operator being defining.
·
Operator overload is classy related to method
overloading
·
When we use any user define datatype with an operator
call operator overloading .
·
Operator overloading is the implementation of compile
time overloading.
·
In a c# with the help of operator function we can
overload the operator.
·
Operator function must be public and static.
·
The ref and out parameters are not allowed in operator
function as argument.
·
Usually we can not change the meaning of an operator.
·
We can not create our new operator there are 2 form of
operator method.
1. Unary
operator
2. Binary
operator
·
We can not overloaded ternary operator.
·
Following is general syntax of operator overload.
public static return type operator op(parameter list)
{
Executable
code
}
·
According to above syntax return type specify the type
of value return by the specific operation.
·
Operator is keyword and op is operator which want to
be overloaded.
·
Following is a list of some operators and its
overloaded ability
Binary
operator : +,
-, *, /, %
Unary
operator : ++,
--
Automatic
overloaded : +=, -=, *=, /=, %=
Etc
operator overloaded : ==, !=, <, >, <=, >=
Not
overloaded : ||, &&, [], (), ?:, ., à, new, sizeof
·
Above are listed operators which is supported by C#.
·
Example of Unary Operator overload
using System;
class Abc
{
int
a,b;
public
void input(void)
{
Console.Write(“Enter
No : “);
a=Convert.ToInt32(Console.ReadLine());
Console.Write(“Enter
No : ”);
b=Conver.ToInt32(Console.ReadLine());
}
public
void output(void)
{
Console.WriteLine(“No
is : “+a);
Console.WriteLine(“No
is : “+b);
}
public
static Abc Abc ++(Abc a1)
{
a1.a=a1.a+10;
a1.b=a1.b+10;
return
a1;
}
}
class OPDemo
{
Abc
a1=new Abc();
a1.input();
a1.output();
++a1;
a1.output();
}
·
According to above example class Abc have two member
function.
·
Two member function and operator function which define
as unary operator overload
·
So It requires only one parameter.
Example of Binary Operator Overload
·
According to above example class have two member
variable, two member function and 1 operator function which define as binary
operator overload so it require two parameter.
3. Abstract
·
Abstract class is one of the behaviour provided by
.Net.
·
Commonly you would like to make a class that only
represent base class and don’t want to create any object of this class type.
·
You can make use of abstract class to implement such a
functionality in C# using modifier abstract.
·
Abstract class may of may not contain abstract method.
·
Abstract method means a method without body.
·
But if class has atleast one abstract method then
class must be declare as a abstract.
·
If class declare abstract it can not be able to
declare instance.
·
To use abstract class you have to inherit from other
class provide implementation of abstract method.
4. Method overriding
·
If sub class has same method as declare in the parent
class it is known as method overriding in C#.
·
In other word if subclass provide specific
implementation of method that has been provided by one of its parent class it
is known as method overriding.
·
Method overriding is used to provide a specific
implementation of method that is already provided by its super class.
·
Method overriding is use for run time polymorphism.
·
Following is some rules for method overriding.
1. Method
must have same name as in the parent class
2. Method
must have same parameter as in the parent class
3. Method
must be is a relationship.
·
Static method does no overriding and same as a main
method.
·
Main method define static so it does not override.
5. Interface inheritance
·
An interface is reference type in C# it is similar to
class.
·
It is a collection of abstract method.
·
Class implement an interface using an inheritance.
·
Along with abstract method and interfaced may also
contain constant default method, static method and nested type.
·
Method body exist only for default method and static
method.
·
Writing an interface similar to writing a class. But
class describe the attributes and behaviour contains behaviour that class
implement.
·
An interface is similar to class because an interface
can contain any number of method and interface is written with file .cs
extension.
·
However an interface is different from class in a
several ways. That is,
1.
You can not instance on interface.
2. Interface
does not contain any constructor.
3.
All of the method in interface are abstract.
·
An interface can inherit with (:) operator so it is
also inherit similar to another class.
Accessmodifier interface interface_name
{
Abstract,constant,variable
Abstract,default,static
method
}
·
According to above syntax interface is a keyword to
define interface in program and name of the interface is must be in a valid
form.
·
It contain a method prototypes without implemented
which is terminated by semicolon.
6. Sealed class
·
Inheritance is concept of reusability and it allow
user to access the property of base class as well as property of same class.
·
But you want to restrict the concert of reusability in
inheritance sealed class are use.
·
Once a class is define as a sealed the class can not
be inherited.
·
In a C# sealed modifier is use to define class as a
sealed.
·
Following is a syntax of sealed keyword.
Access modifier sealed class classname
{
Executable
code
AccessModifire
returntype methodname(argument)
{
Executable
Code
}
}
·
According to above syntax we can not inherit sealed
class. It means sealed class does not have childclass.
·
It means it can’t be a base class even it can not be
abstract class. If we try to derived a property from sealed class compiler
through on error.
·
The second use of the sealed keyword is to prevent
method overriding. It means if the sealed keyword is in base class method than
derived class not allow to access the same method for overriding.
·
Sealed keyword may be use for security reason or may
last class in a hierarchy.
Following is example of sealed class & sealed
method.
·
According to above example class A contain sealed
method so class define a sealed class so no one class can inherit it. So here
we prevent overriding as well as inheritance.
7. Creating and using pointer (Unsafe concept)
·
Pointer is a variable with unique features. A pointer
variable hold the address of another variable. Pointer hold address of only
array and value type.
·
C# support pointering limited extend an like a
reference type. Pointer type are not track by the default garbage collection
mechanism and for the same reason pointers are not allow to point to a
reference type or even to stricter type which contain a reference type.
·
We can say that pointer can point only unmanaged type
which include all basic datatype, enum type and pointer types.
·
Following is some list of pointer operator.
& : it is
address of operator. It return a pointer that return the address of
the
variable.
* : dereference operator it return the
value where the pointer point.
à : it is
pointer to member operator. It is shortcut for access member.
Unsafe keyword :
·
In C# whatever the code we write is default “safe”
code.
·
Unsafe code allow as to work with direct memory and it
create a problem to work with memory so we write code as a unsafe.
·
Unsafe code run outside the control of garbage
collection it means unsafe code are not verifiable by the common language
runtime.
·
It is compulsory to write unsafe keyword when we use
pointer.
·
Following is an example of pointer with unsafe
keyword.
Examples.
·
According to above example pointer is used with array
so manage pointer variable during the execution statement use fixed keyword to
instruct garbage collector not to move certain object in a managed code.
8. Creating and using
property
·
Properties are name member of class, structure and
interface.
·
Member variable or method in a class or structure
called field. Properties are an extension of fields and are access using the
same syntax.
·
They use accessor throw which the values of the
private field can be read, write or manipulate.
·
Properties do not name the storage location. Instaned
they have accessors that read, write or compute their value.
·
For example a class contain private field like rollno,
name, age etc... and we can not directly access this field from outside the
class scope but we can have properties for accessing this private fields.
·
The accessor of property contain the executable
statement that help in getting or setting properties.
·
There are two type of accessors.
1. get
2. set
examples
·
According to above example student class contain 3
private member variable and also it contain 3 different properties with get and
set accessor which allow to read and write in private member field in the
class.
9. Creating an Using
Indexer
·
An indexer allowed an object to be index such as an
array.
·
When you define an indexer for class, this class behave
a similar to virtual array.
·
Indexer is use to access the class instance using
index.
·
It is use for handling an object as an array. It give
effect array like to the class. It is refer as a smart array.
·
Defining a C# indexer is similar to define properties.
·
Syntax:
public type this[type identifier]
{
get{}
set{}
}
·
According to above syntax get and set accessor are
implicitly used when you assign the class instance element in the same way as
you can assign element in an array.
·
In above syntax indexers are not define with name but
with this keyword which refer to an object instance.
Examples
·
According to above example class student contain
string array to define 5 student name using get and set indexer and in a main
function class object behave like an array and pass integer index to set the
value and using a loop we can get all the value which are already set in string
array.
Difference
Indexer
|
Properties
|
It is used by its signature
|
It is used by its name
|
Indexer does not require a name and it is
access using this
|
Property of class should have a unique
name
|
It must be an instance member
|
It either static or instance member
|
Indexer should be associated with
parameter
|
Property can not have parameter.
|
10. Creating and using delegate (single / multicast)
·
Delegates are similar to C++ function pointer. It is
refer to as a type safe function pointer. We can say it is smart version of
function pointer.
·
It is a types and object that can point or more method
in the application which can be invoke letter. It holds method reference in an
object.
·
Delegate calls function at runtime based on dynamic
condition. Here function can be assign like a variable.
·
It allows method to be pass as a parameter.
·
When we create delegate it need method name and with
its parameter or without parameter.
·
Remember that the delegate where it point those method
have same signature and parameter type.
·
Delegate can be chain to gather delegate improve the
performance of application delegate can invoke a method asynchronizely.
·
Syntax : public
delegate returntype delegatename(argument);
·
According to above syntax public is access specifier
and delegate use public specifier because it work with different class.
Delegate is a keyword type name specify the return type and delegate name is a
name of the delegate either it may contain argument or void.
Example
·
According to above example Ddemo is delegate which
contain method reference in int from the main function it call using its object
d1 and d2 respectively.
Multicasting Delegate :
·
Delegate ability to multicast means that delegate
object can hols the address of many functions and maintain list of method to
call rather than a single method.
·
In the multicast delegate when we invoke delegate it
call all the function which are in the list of it.
·
When we can’t to add a method to the invocation list
of a delegate object simply use += to add a function in to sequence and -= to
remove a function from the sequence.
11. Create and using
Events with Delegate
·
Microsoft window is an event driven operation system.
·
Int eh event driven any task perform using on an
event.
·
Event is nothing but a reaction of anything or
reaction of any action.
·
Events are fire by user action program call or by the
system.
·
For example, mouse click on button.
·
Event procedure or Event method is declaring using
delegates.
·
Event have source and listener source determine when
event is raised listener are work in what action is taken in response to the
event.
·
Public void eventname(object sender Event e)
·
According to above syntax sender help to get
information who is responsible for raised this event and “e” contain information
for the current event.
·
Example
12. Collections
·
Collection is one dimentional storage area collection
contain in which values of mix datatypes can be place.
·
Collections are increase in size simply by adding
items to it. Collection like a link list in a data structure.
·
Collections are part of System.Collections namespace.
·
Before use of any collection we must import collection
namespace. The collection have add, remove, remove all contain, count and item
method. Etc...
·
Each item is a collection can have both a value and
key. Where key is used as a index to it’s associated value usually we use
foreach loop to navigate element of collection.
·
Following is some of list of collection arraylist,
hashtable, stack, queue sortedlist...
ArrayList :
·
Arraylist is a collection of object if we face trouble
on error when.
·
Working an dealing with the size of the array then use
arraylist.
·
It implement Ilist interface. It can grow dynamically.
·
We can add element in it at runtime arraylist is used
for heterogeneous data.
·
The data in the arryalist need not be of some datatype.
Arraylist is more dynamic you can add or remove items without losing the
performance.
Example.
Hashtable :
·
Hashtable is storing data in key-value pair where key
field will remain unique element can be search using key-value.
·
ArrayList access there element by there index but
hashtable allow to access item by the key. Because a each item has a value and
a key.
·
The value is the same value you store in any array. But
key is a meaningful entity for accessing the items in collection.
·
Hashtable has duplicate value but key must be unique.
·
It search data very fast in compare to other collections
but it require high memory
usage.
Example.
Stack :
·
The stack data structure implement in LIFO system. It
means last in first out manner.
·
It is used when you need a Last In First Out access of
items.
·
When you add an items in the list it call pushing the
item. And when you remove an item from the list it is call popping. The item.
·
The real life example of stack is plats in table.
Example.
Queue :
·
The queue data structure implement in FIFO system it
means first in first out manner.
·
It is used when you need a first in first out access
of items.
·
When you add on items in the list it is call Enqueue
the item.
·
And when you remove an item from the list it call
Dequeue the item.
·
The real life example of queue is ticket window.
SortedList :
·
SortedList is the combination of an array and hashtable.
·
List can be access either by an index or with a key.
·
The name suggests is store data in assorted manner,
the items of sorted list are order according to the value of their keys and there
is no method for storing the collections according the value.
·
The sortedList internally maintain to array to store
the element of the list that is one array for the keys and another array for
its associated value.
·
A sortedList may not contain duplicate keys but hashtable
can have a duplicate keys.
Example.
:: Best of Luck ::
https://myshopprime.com/uday.shah3/shop