Hi Friends,
This post is regarding the Most
Confusing Questions in .Net, C#.Net, ASP.Net and Sql Server. As I have seen
that lot of people whether they are searching for jobs, already in the job or
they try to understand the things, gets lot of confusions and there are very
rare sites where they explain the things in more detailed and example
way.
So I thought to write such post which
will be helpful for all of us to try to understand the things in better manner
and it will be helpful whether we are in search of job or in actual
implementation in our projects:
1.
Abstract classes provide a simple and easy way to version your components. By
updating the base class, all inheriting classes are automatically updated with
the change. How?
Ans. Abstract class is the collection of abstract and concrete members. I
am not calling here method or function but members because members can be
methods or properties or indexers etc.
A concrete member means non-abstract or general members.
Versioning of a class means adding or removing the members from the class.
So as per question statement, in abstract class we can add or remove the members and it will not impact your classes which are inherited by the abstract class. The only thing is if the class doesn't contain non-abstract members. As the abstract class can contain both abstract and concrete members so it hardly matters to add or remove any non-abstract members.
So we can make another version of the class by adding or removing members in the class. So if we update the base class or the abstract class by adding or removing any non-abstract member, the child class will have no impact, it will be automatically inheriting the new members.
A concrete member means non-abstract or general members.
Versioning of a class means adding or removing the members from the class.
So as per question statement, in abstract class we can add or remove the members and it will not impact your classes which are inherited by the abstract class. The only thing is if the class doesn't contain non-abstract members. As the abstract class can contain both abstract and concrete members so it hardly matters to add or remove any non-abstract members.
So we can make another version of the class by adding or removing members in the class. So if we update the base class or the abstract class by adding or removing any non-abstract member, the child class will have no impact, it will be automatically inheriting the new members.
public abstract class
MyClass
{
public abstract void Show();
public int Add(int a, int b)
{
return a+b;
}
}
{
public abstract void Show();
public int Add(int a, int b)
{
return a+b;
}
}
This is the abstract class and we can
inherit it to the child classes.
Let’s say:
Let’s say:
class ChildClass:MyClass
{
public override void Show()
{
// some implementation
}
}
{
public override void Show()
{
// some implementation
}
}
Now we can create another version of abstract
class by adding an extra method;
public abstract class
MyClass
{
public abstract void Show();
public int Add(int a, int b)
{
return a+b;
}
public int Sub(int a, int b)
{
return a-b;
}
}
{
public abstract void Show();
public int Add(int a, int b)
{
return a+b;
}
public int Sub(int a, int b)
{
return a-b;
}
}
Now it hardly matters for the child
class to modify anything.
2. If a new version of an interface is required, you must create a whole new interface.
Ans. For interface which is the collection of abstract members is less
flexible. Because when we need to create another version of interface, means
adding or removing a member which is abstract so we need to change all the
child class either remove or adding override members.
Let's see with the example:
Let's see with the example:
interface MyInf
{
void Show();
void Print();
}
{
void Show();
void Print();
}
Now use this interface in our class:
class ChildClass: MyInf
{
public void Show()
{
// some implementation
}
public void Print()
{
// some implementation
}
}
{
public void Show()
{
// some implementation
}
public void Print()
{
// some implementation
}
}
Now if we add another member in the
interface, we need to implement it to all our child classes so it is preferable
to create another interface and inherit that as below:
interface MyInf2
{
void Display();
}
{
void Display();
}
Use this in the class:
class ChildClass:
MyInf,MyInf2
{
public void Show()
{
// some implementation
}
public void Print()
{
// some implementation
}
public void Display()
{
// some implementation
}
}
{
public void Show()
{
// some implementation
}
public void Print()
{
// some implementation
}
public void Display()
{
// some implementation
}
}
3.
What are the advantages of properties (get, set)? When we go for
properties? What is the difference with using properties and without using
properties?
Ans: Properties are the way to
carry the data from one class to another class, from one application to another
application etc.
When we want to send the data from one
class to another class or event one application to another application, we can
set the value and get its value to another place.
We can do our tasks without using the
properties and in that case we need to send our data as the parameters and get
it to another class as below:
class A
{
public int IsValid(int val1, int val2, string
val3, string val4)
{
B objB = new B();
int result=
objB.Validate(val1,val2,val3,val4);
}
}
Now in the class B, we will have the Validate method and will do the validation
based on the input values:
class B
{
public int Validate(int val1, int val2, string
val3, string val4)
{
// some processing
}
return true;
}
here we can see that we have not used
the properties for sending the data from class A to class B. And it will work
fine.
Now we will try the same task using the
properties. We will create a class which will contain all the properties as
below:
public class
MyProperties
{
public int Val1{get;set;}
public int Val2{get;set;}
public string Val3{get;set;}
public string Val4{get;set;}
}
Now we will try to set these value and
pass this whole object to another class:
class A
{
public int IsValid(int val1, int val2, string
val3, string val4)
{
var objMyProperties= new MyProperties()
{
Val1 = val1,
Val2 = val2,
Val3 = val3,
Val4 = val4,
};
B objB = new B();
int result= objB.Validate(objMyProperties);
}
}
And your B class will be:
class B
{
public int Validate(MyProperties
objProperties)
{
var value1 = objProperties.Val1;
var value2 = objProperties.Val2;
var value3 = objProperties.Val3;
var value4 = objProperties.Val4;
// some processing
}
return true;
}
Here we can see that if we are adding
additional properties or removing the existing properties, our call will not
affect in class A and then method signature will also be same in class B.
But in the first case, if we remove one
parameter or add additional parameter, we need to make the changes in the Class
A calling method as well as the method signature in the Class B also.
4. Can we write goto statement in
Finally block.
Ans: No. We can’t write the goto statement
in finally block. We can write the goto statement in try or in catch block
because finally block must execute all the statements inside whether
the exception raised or not. So the control can't move to outside
from finally block.
If we try to do, it will show the
compile time error saying "Control cannot leave the body of finally
clause".
5. Two class nested ,can we call upper class in inner class?
Ans: Yes, we can class the upper class
inside the inner class.
6. Can get and set method have another access modifier ?
6. Can get and set method have another access modifier ?
7. Can class declare protected?
Ans: No. The class can NOT be declared as private, protected or protected internal. Not only the class, any elements inside the namespace can't be declared as private, protected or protected internal. We can declare the class either internal or public.
Ans: No. The class can NOT be declared as private, protected or protected internal. Not only the class, any elements inside the namespace can't be declared as private, protected or protected internal. We can declare the class either internal or public.
If we create a class with the private
or protected modifier, it will show the compile time error:
8. Can abstract class have constructor?
Ans: Yes, abstract class can also have constructor where we can initialize the members.
Ans: Yes, abstract class can also have constructor where we can initialize the members.
9. Can a instance class have static constructor?
Ans: Yes, the instance class can have static constructor.