Category OOPS

Introduction to Object Oriented Programming Concepts (OOPS) in C#.net

Category: ASP.NETC#.NetOOPS Comments: No comments

What is OOP? OOP is a design philosophy. It stands for Object Oriented Programming. Object-Oriented Programming (OOP) uses a different set of programming languages than old procedural programming languages (C, Pascal, etc.). Everything inOOP is grouped as self sustainable "objects". Hence, you gain re-usability by means of four main object-oriented programming concepts. In order to clearly understand the object orientation, let’s take your “hand” as an example. The “hand” is a class. Your body has two objects of type hand, named left hand and right hand. Their main functions are controlled/ managed by a set of electrical signals sent through your shoulders (through an interface). So the shoulder is an interface which your body uses to interact with your hands. The hand is a well architected class. The hand is being re-used to create the left hand and the right hand by slightly changing the properties of it. What is an Object? An object can be considered a "thing" that can perform a set of related activities. The set of activities that the object performs defines the object's behavior. For example, the hand can grip something or a Student (object) can give the name or address.Read More

Difference between Struct and Class in C#.net

Category: C#.NetOOPS Comments: No comments

Difference between Struct and Class

  • Struct are Value type and are stored on stack, while Class are Reference type and are stored on heap.
  • Struct “do not support” inheritance, while class supports inheritance. However struct can implements interface.
  • Struct should be used when you want to use a small data structure, while Class is better choice for complex data structure.
  •  You cannot have instance Field initializers in structs.But classes can have
    example:
    class MyClass
    {
    int myVar =10; // no syntax error.
    public void MyFun( )
    {
    // statements
    }
    }
    struct MyStruct
    {
    int myVar = 10; // syntax error.
    public void MyFun( )
    {
    // statements
    }
    }
  • When passing a class to a method, it is passed by reference. When passing a struct to a method, it’s passed by value instead of as a reference.
  • classes are used for complex and large set data. structs are simple to use.
  • We can assign null variable to class. But we cannot assign null to a struct variable, since structs are value type.
  • We can declare a destructor in class but can not in struct.
Read More

Difference between Ref and Out Keyword in C#.net

Category: C#.NetOOPS Comments: No comments

Introduction Both the parameters passed by reference, While for the Ref Parameter you need to initialize it before passing to the function and out parameter you do not need to initialize before passing to function. you need to assign values into these parameter before returning to the function. Ref (initialize the variable) int getal = 0; Fun_RefTest(ref getal); Out (no need to initialize the variable) int getal; Fun_OutTest(out getal); The out and the ref parameters are used to return values in the same variables, that you pass an an argument of a method. These both parameters are very useful when your method needs to return more than one values.Read More

Difference between Boxing and Un-Boxing in C#.net

Category: C#.NetOOPS Comments: No comments

Boxing and Un-Boxing

Boxing: means converting value-type to reference-type.

Eg:

int I = 20;

string s = I.ToSting();

 

UnBoxing: means converting reference-type to value-type.

Eg:

int I = 20;

string s = I.ToString(); //Box the int

int J = Convert.ToInt32(s); //UnBox it back to an int.

 

Note: Performance Overheads due to boxing and unboxing as the boxing makes a copy of value type from stack and place it inside an object of type System.Object in the heap.

Read More

Difference between Value and Reference type in C#.net

Category: C#.NetOOPS Comments: No comments

Value Type and Reference Type

A variable is value type or reference type is solely determined by its data type.

Eg: int, float, char, decimal, bool, decimal, struct, etc are value types, while object type such as class, String, Array, etc are reference type.

 

Value Type

As name suggest Value Type stores “value” directly.Memory is allocated at compile time.

For eg:

//I and J are both of type int

I = 20;

J = I;

int is a value type, which means that the above statements will results in two locations in memory.

For each instance of value type separate memory is allocated.

Stored in a Stack.

It Provides Quick Access, because of value located on stack.

 

Reference Type

As name suggest Reference Type stores “reference” to the value.Memory is allocated at run time

For eg:

Vector X, Y; //Object is defined. (No memory is allocated.)

X = new Vector(); //Memory is allocated to Object.//(new is responsible for allocating memory.)

X.value = 30; //Initialising value field in a vector class.

Y = X; //Both X and Y points to same memory location.//No memory is created for Y.

Console.writeline(Y.value); //displays 30, as both points to same memory

Y.value = 50;

Console.writeline(X.value); //displays 50.

Note: If a variable is reference it is possible to indicate that it does not refer to any object by setting its value to null;

Reference type are stored on Heap.

It provides comparatively slower access, as value located on heap.

Read More

Introduction of Sealed Class in C#.net

Category: C#.NetOOPS Comments: No comments

Sealed Classes

Sealed classes are classes that can’t be derived from. To prevent other classes from inheriting from a class, make it a sealed class. There are a couple good reasons to create sealed classes, including optimization and security.

Sealing a class avoids the system overhead associated with virtual methods. This allows the compiler to perform certain optimizations that are otherwise unavailable with normal classes.

Another good reason to seal a class is for security. Inheritance, by its very nature, dictates a certain amount of protected access to the internals of a potential base class. Sealing a class does away with the possibility of corruption by derived classes. A good example of a sealed class is the String class. The following example shows how to create a sealed class:

public sealed class CustomerStats
{
string gender;
decimal income;
int numberOfVisits;
public CustomerStats()
{
}
}
public class CustomerInfo : CustomerStats // error
{
}

This example generates a compiler error. Since the CustomerStats class is sealed, it can’t be inherited by the CustomerInfo class.The CustomerStats class was meant to be used as an encapsulated object in another class. This is shown by the declaration of a CustomerStats object in the Customer class.

public class Customer
{
CustomerStats myStats; // okay
}

Read More

Introduction of Static Keyword in C#.net

Category: C#.NetOOPS Comments: No comments

Static Class : A static class is you can say same as the non-static class, but there is one difference them is a static class can't be instantiated. In other words, you cannot use the "new" keyword to create a instance variable of the class type. As there is no instance variable, you can access the members of a static class by using the name of the class itself. Static classes and it's class members are used to create data and functions that can be accessed without creating an instance of the class. Static class members can be used to separate data and behavior that is independent of any object identity: the data and functions do not change regardless of what happens to the object. Static classes can be used when data or behavior is not present in the class that depends on object identity. Static classes are loaded automatically by the .NET Framework CLR when that program or namespace containing the class is loaded. The main features of a static class are: static classes can only contain static members. static classes can not be instantiated. static classes are sealed and therefore cannot be inherited.Read More