1.What is the difference between an object and a class?
1.An object is an instance of a class. A class is the definition of an object.
2.Class is user defined data type it contain data member and function. Object is real time entity.
3.Class is a collection of data members and methods. Object is an instance of a class.
4.class is only declaration.While object is actual creation of memory space for that declaration.
5.Blueprint for how to build a specific car.Object: An actual car (instance) created from the car blueprint (the class).
2.What is difference between overloading and overriding?
http://www.advancesharp.com/blog/1056/overloading-and-overriding-in-c-with-example
In the above example ,their are three methods with same method same but they differ with number of parameters and type of parameters ,hence this types of methods is called as method overloading.
Output of the above Program is
5. What’s difference between Reference Type and Value Type?
http://www.codeproject.com/Articles/76153/Six-important-NET-concepts-Stack-heap-value-types
9. Difference between Finalize() and Dispose() methods in .NET
Finalize:
1. Finalize() belongs to the Object class.
2. It is automatically called by the Garbage Collection mechanism when the object goes out of the scope(usually at the end of the program
3. It is slower method and not suitable for instant disposing of the objects.
4. It is non-deterministic function i.e., it is uncertain when Garbage Collector will call Finalize() method to reclaim memory.
5. Example:
class employee
{
//This is the destructor of emp class
~employee()
{
}
//This destructor is implicitly compiled to the Finalize method.
}
Dispose:
1. Dispose() belongs to the IDisposable interface
2. We have to manually write the code to implement it(User Code)
ex: if we have emp class we have to inherit it from the IDisposable interface
and write code. We may have to suppress the Finalize method using GC.SuppressFinalize() method.
3. Faster method for instant disposal of the objects.
4. It is deterministic function as Dispose() method is explicitly called by the User Code.
5. Example:
1.An object is an instance of a class. A class is the definition of an object.
2.Class is user defined data type it contain data member and function. Object is real time entity.
3.Class is a collection of data members and methods. Object is an instance of a class.
4.class is only declaration.While object is actual creation of memory space for that declaration.
5.Blueprint for how to build a specific car.Object: An actual car (instance) created from the car blueprint (the class).
2.What is difference between overloading and overriding?
http://www.advancesharp.com/blog/1056/overloading-and-overriding-in-c-with-example
Overloading
|
Overriding
|
Having same method name with different Signatures (parameters).
|
Methods name and signatures must be same.
|
Overloading is the concept of compile time polymorphism.
|
Overriding is the concept of run time polymorphism.
|
Two functions having same name and return type, but with different type and/or number of arguments is called as Overloading.
|
When a function of base class is re-defined in the derived class called as Overriding.
|
It doesn't need inheritance.
|
It needs inheritance.
|
Method can be different access modifiers.
|
Method should be public.
|
- Overloading = Multiple method signatures, same method name
- Overriding = Same method signature (declared virtual), implemented in sub classes
What is Method Overloading ?
Creating a multiple methods in a class with same name but different parameters and types is called as method overloading.method overloading is the example of Compile time polymorphism which is done at compile time.
Method overloading can be achieved by using following things :
- By changing the number of parameters used.
- By changing the order of parameters.
- By using different data types for the parameters.
e.g
- public class Methodoveloading
- {
- public int add(int a, int b) //two int type Parameters method
- {
- return a + b;
- }
- public int add(int a, int b,int c) //three int type Parameters with same method same as above
- {
- return a + b+c;
- }
- public float add(float a, float b,float c,float d) //four float type Parameters with same method same as above two method
- {
- return a + b+c+d;
- }
- }
FAQ of Method Overloading
Question- Can method overloading has same numbers of parameters and Name with different return types?
Answer- No, because conflict is happen in methods while passing the parameters .
What is Method overriding ?
Creating the method in a derived class with same name, same parameters and same return type as in base class is called as method overriding.
Method overriding is the example of run time polymorphism,how its is the part of run time polymorphism i will explain in detail.
Some Key Points of Method overriding
- Method overriding is only possible in derived class not within the same class where the method is declared.
- Only those methods are overrides in the derived class which is declared in the base class with the help of virtual keyword or abstract keyword.
e.g
- public class Account
- {
- public virtual int balance()
- {
- return 10;
- }
- }
- public class Amount:Account
- {
- public override int balance()
- {
- return 500;
- }
- }
- 10 and 500
The Method overriding is very useful when we wants to return different output of same method in different class according to the need.
Let us consider the example I wants to provide the discount on particular product according to the Customer category that A,B,C in this scenario suppose A,B,C are the classes and Discount is the class which contains virtual CustDiscount () method ,then i simply override it on class A,B,C instead of writing three different methods for each class.
3.What’s difference between Interface and Abstract Class?
- An Abstract class doesn't provide full abstraction but an interface does provide full abstraction; i.e. both a declaration and a definition is given in an abstract class but not so in an interface.
- Using Abstract we can not achieve multiple inheritance but using an Interface we can achieve multiple inheritance.
- We can not declare a member field in an Interface.
- We can not use any access modifier i.e. public , private , protected , internal etc. because within an interface by default everything is public.
- An Interface member cannot be defined using the keyword static, virtual, abstract or sealed.
4. What’s difference between Class and Structure?
- Structures are value types and the classes are reference types. Before proceeding to the next point, let explain the difference between the two types. Imagine this is the memory within the machine:
Figure 1
The value types are stored in the stack but the reference types are not. In fact, what could be really stored in the stack for the reference types is a pointer that targets an address at the heap level.
Then the type of structure objects are stored in the stack exactly like any value type, say an integer, a double or a float. Meanwhile, memory locations could be reserved for reference types in the heap. Defining the heap and stack and the difference between them is beyond the scope of this article but nevertheless I propose this excellent Matthew article to understand the memory mechanisms.
http://www.c-sharpcorner.com/UploadFile/rmcochran/csharp_memory01122006130034PM/csharp_memory.aspx?ArticleID=9adb0e3c-b3f6-40b5-98b5-413b6d348b91 - Classes are usually used for large amounts of data, whereas structs are usually used for smaller amounts of data.
- Classes can be inherited whereas structures not.
- A structure couldn't be null like a class.
- A structure couldn't have a destructor such as a class.
- A structure can't be abstract, a class can.
- You cannot override any methods within a structure except the following belonging to the type object:
- Equals()
- GetHashCode()
- GetType()
- ToString()
And the other polymorphism technique used for structures is implementing interfaces. - Declared events within a class are automatically locked and then they are thread safe, in contrast to the structure type where events can't be locked.
- A structure must always have the default parameter less constructor defined as public but a class might have one, so you can't define a private parameter-less constructor as in the following:
struct Me {
private Me()// compile-time error {
}
}
class Me {
private Me()// runs Ok{
} - A static constructor is triggered in the case of a class but not in the case of a structure as in the following:
struct myStructure {
static myStructure()
{
Console.WriteLine("This is me a structure");
}
}
class myClass{
static myClass()
{
Console.WriteLine("This is me a class");
}
}class Program {
static void Main(string[] args)
{
myStructure s =new myStructure();//Nothing happen myClass c =new myClass();//Will out put This is me a class Console.Read();
}
}
- The strucutre can't conatain a volatile field whereas the class can
- You can't use sizeof with classes but you can with structures
- Fields are automatically initialized with classes to 0/false/null wheatheras strucutres are not
- Fields can't be directley instantiated within structures but classes allow such operations as in the following:
struct myStructure {
publicstring x = 2;//Not allowed }
class myClass {
publicstring x = 2;//Allowed }
- Structures and classes don't adopt the same aproach for the System.Object.Equals() method.
Assume the following strucutre and class:
struct StructurePerson {
publicstring FirstName;
publicstring LastName;
}
class ClassPerson {
publicstring FirstName;
publicstring LastName;
}Now, try this code:
class Program {
static void Main(string[] args)
{
StructurePerson strX =new StructurePerson();
strX.LastName = "Bejaoui";
strX.FirstName = "Bechir";
StructurePerson strY =new StructurePerson();
strY.LastName = "Bejaoui";
strY.FirstName = "Bechir";if (strX.Equals(strY))
{
Console.WriteLine("strX = strY");
}
else {
Console.WriteLine("strX != strY");
}//This code displays strX = strYClassPerson clsX =new ClassPerson();
clsX.LastName = "Bejaoui";
clsX.FirstName = "Bechir";
ClassPerson clsY =new ClassPerson();
clsY.LastName = "Bejaoui";
clsY.FirstName = "Bechir";if (clsX.Equals(clsY))
{
Console.WriteLine("clsX = clsY");
}
else {
Console.WriteLine("clsX != clsY");
}//This code displays clsX != clsY Console.Read();
} }
In the first strucutre the two objects are value types, they are compared depending on their values like int I = 5 and int J = 5 so I=J because they have the same value. In the contrast, in the class case of two different and distinct references, to make clsX = clsY you should use the following code:
ClassPerson clsX =new ClassPerson();
clsX.LastName = "Bejaoui";
clsX.FirstName = "Bechir";
ClassPerson clsY = clsX;
if (clsX.Equals(clsY))
{
Console.WriteLine("clsX = clsY");
}
else {
Console.WriteLine("clsX != clsY");}//This code displays clsX = clsY
http://www.codeproject.com/Articles/76153/Six-important-NET-concepts-Stack-heap-value-types
Value type:
Holds some value not memory addresses
Example:
Struct
Storage:
Value types are stored on stack.
Advantages:
A value type does not need extra garbage collection. It gets garbage collected together with the instance it lives in. Local variables in methods get cleaned up upon method leave.
Drawbacks:
- When large set of values are passed to a method the receiving variable actually copies so there are two redundant values in memory.
- As classes are missed out.it losses all the oop benifits
Reference type:
Holds a memory address of a value not value
Example:
Class
Storage:
Stored on heap
Advantages:
- When u pass a reference variable to a method and it changes it indeed changes the original value whereas in value types a copy of the given variable is taken and that's value is changed.
- When the size of variable is bigger reference type is good
- As class comes in this type of variable oops is promoted so it gives reusability those oop benifits
Drawbacks:
More work referencing when allocating and dereferences when reading the value.extra overload for garbage collector
6. What are the Access Modifiers in C# ?
Different Access Modifier are - Public, Private, Protected, Internal, Protected Internal
- Public – When a method or attribute is defined as Public, it can be accessed from any code in the project. For example, in the above Class “Employee” getName() and setName() are public.
- Private - When a method or attribute is defined as Private, It can be accessed by any code within the containing class only. For example, in the above Class “Employee” attributes name and salary can be accessed within the Class Employee Only. If an attribute or class is defined without access modifiers, it's default access modifier will be private.
- Protected - When attribute and methods are defined as protected, it can be accessed by any method in the inherited classes and any method within the same class. The protected access modifier cannot be applied to classes and interfaces. Methods and fields in a interface can't be declared protected.
- Internal – If an attribute or method is defined as Internal, access is restricted to classes within the current project assembly.
- Protected Internal – If an attribute or method is defined as Protected Internal, access is restricted to classes within the current project assembly and types derived from the containing class.
7. Static constructor
A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced.
Static constructors have the following properties:
- A static constructor does not take access modifiers or have parameters.
- A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
- A static constructor cannot be called directly.
- The user has no control on when the static constructor is executed in the program.
- A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
- Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the
LoadLibrarymethod.
Things to know about Static Constructor
- It is used to initialize static data members.
- Can't access anything but static members.
- Can't have parameters
- Can't have access modifiers like Public, Private or Protected.
8. Private constructor
Important points of private constructor
- One use of private construct is when we have only static member.
- Once we provide a constructor that is either private or public or any, the compiler will not allow us to add public constructor without parameters to the class.
- If we want to create object of class even if we have private constructors then we need to have public constructor along with private constructor
When you want to prevent the users of your class from instantiating the class directly. Some common cases are:
- Classes containing only static methods
- Singletons
When we declare a class constructor as private , we can not do 2 things:-
- We can not create a object of the class.
- We can not inherit the class.
9. Difference between Finalize() and Dispose() methods in .NET
Finalize:
1. Finalize() belongs to the Object class.
2. It is automatically called by the Garbage Collection mechanism when the object goes out of the scope(usually at the end of the program
3. It is slower method and not suitable for instant disposing of the objects.
4. It is non-deterministic function i.e., it is uncertain when Garbage Collector will call Finalize() method to reclaim memory.
5. Example:
class employee
{
//This is the destructor of emp class
~employee()
{
}
//This destructor is implicitly compiled to the Finalize method.
}
Dispose:
1. Dispose() belongs to the IDisposable interface
2. We have to manually write the code to implement it(User Code)
ex: if we have emp class we have to inherit it from the IDisposable interface
and write code. We may have to suppress the Finalize method using GC.SuppressFinalize() method.
3. Faster method for instant disposal of the objects.
4. It is deterministic function as Dispose() method is explicitly called by the User Code.
5. Example:
//Implement Dispose Method.
public class TestDispose : IDisposable
{
private bool disposed = false;
//Implement IDisposable.
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
// clean unmanged objects
}
// clean unmanaged objects).
disposed = true;
}
}
}
No comments:
Post a Comment