Difference between Abstract Class and Interface?
Abstract class is a class which can’t be instantiated and which can have methods with definition as well as declaration also. This can be inherited.
As for Example:
public abstract class AbstractStudent
{
String Roll
{
get;
set;
}
String FirstName
{
get;
set;
}
String LastName
{
get;
set;
}
Public String GetStudentDetails()
{
// Implementation of Method
}
public String SaveStudentDetails ()
{
// Implementation of Method
}
public abstract String CalculateWage();
}
Interface can only contain the methods declaration and can be implemented in the class.
As for Example:
Public interface IStudnet
{
String Roll
{
get;
set;
}
String FirstName
{
get;
set;
}
String LastName
{
get;
set;
}
String GetStudentDetails();
String SaveStudentDetails ();
}
Below are the few main difference between Abstract Class and Interface
a. In abstract class method can have definition as well as declaration also. But Interface should have only definition.
b. All the Methods are Public as default and don’t have any access Modifier Controls in interface, whereas for abstract class we can have access modifier for methods.
c. Abstract class can have constructor or destructor, whereas interface not.
d. Abstract class can’t be part of multiple inheritance and we can implement multiple interface.
What do you mean by String objects are immutable?
String objects are immutable as its state cannot be modified once created. Every time when we perform any operation like add, copy, replace, and case conversion or when we pass a string object as a parameter to a method a new object will be created.
Example:
String str = “ABC”;
str.Replace(“A”,”X”);
Here Replace() method will not change data that “str” contains, instead a new string object is created to hold data “XBC” and the reference to this object is returned by Replace() method.
So in order to point str to this object we need to write below line.
str = str.Replace(“A”,”X”);
Now the new object is assigned to the variable str. earlier object that was assigned to str will take care by garbage collector as this one is no longer in used.
What is dll hell problem in .NET and how it will solve?
Ans: Dll hell, is kind of conflict that occurred previously, due to the lack of version supportability of dll for (within) an application
.NET Framework provides operating system with a global assembly cache. This cache is a repository for all the .net components that are shared globally on a particular machine. When a .net component installed onto the machine, the global assembly cache looks at its version, its public key and its language information and creates a strong name for the component. The component is then registered in the repository and indexed by its strong name, so there is no confusion between the different versions of same component, or DLL
What is a Partial class?
Ans: Instead of defining an entire class, you can split the definition into multiple classes by using partial class keyword. When the application compiled, c# compiler will group all the partial classes together and treat them as a single class. There are a couple of good reasons to use partial classes. Programmers can work on different parts of classes without needing to share same physical file
Ex:
Public partial class employee
{
Public void somefunction()
{
}
}
Public partial class employee
{
Public void function ()
{
}
}
What is difference between constants, read-only and, static?
Constants: The value can’t be changed
Read-only: The value will be initialized only once from the constructor of the class.
Static: Value can be initialized once.
What is the cross page post backing?
Asp.Net 2.0 fixed this with built-in features that allowed us to easily send information from one page to another.
Button control has property PostBackUrl that can be set to URL of any page in our ASP.Net WebSite where we want to transfer all form values to.
Along with that Asp.Net 2.0 Page class has a property PreviousPage that allows us to get reference to the Page object that initiated the postback (in other words to get the actual reference to the Page object of the aspx page on which user clicked the Submit button on a HTML form).
So for example lets create two sample pages in our Web Application:
SourcePage.aspx:
<formid=”form1″runat=”server”>
<div>
First Name: <asp:TextBoxID=”FirstName”runat=”server”></asp:TextBox><br />
Last Name: <asp:TextBoxID=”LastName”runat=”server”></asp:TextBox><br /><br />
<asp:ButtonID=”Button1″runat=”server”Text=”Submit To Destination Page”PostBackUrl=”~/CrossPagePostbacks/DestinationPage.aspx” />
</div>
</form>
When our user clicks the Submit button, all the values from the HTML Form on SourcePage.aspx will be transfered to the DestinationPage.aspx and we will also be able to get reference to the SourcePage.aspx in our DestinationPage with the PreviousPage property like this:
So in our DestinationPage.aspx.cs code-behind we can easily access two TextBox controls on SourcePage.aspx and show them in two label controls like this:
protectedvoid Page_Load(object sender, EventArgs e)
{
// first check if we had a cross page postback
if ( (PreviousPage != null) && (PreviousPage.IsCrossPagePostBack) )
{
Page previousPage = PreviousPage;
TextBox firstName = (TextBox)previousPage.FindControl(“FirstName”);
TextBox lastName = (TextBox)previousPage.FindControl(“LastName”);
// we can now use the values from TextBoxes and display them in two Label controls..
labelFirstName.Text = firstName.Text;
labelLastName.Text = lastName.Text;
}
}
You probably noticed that we first checked if PreviousPage property of current page (DestinationPage.aspx) is NOT NULL, this is done to avoid running our code in case that user opens our DestinationPage.aspx directly, without running a cross page postback.
Also here we checked the another PreviousPage property called IsCrossPagePostBack to see if we really had a CrossPagePostback.
(If Server.Transfer is used to redirect to this page, IsCrossPagePostBack property will be set to FALSE.
TIP: We can be completely sure that we have a real CrossPagePostback ONLY IF:
Now this is very useful and i’m sure you are eager to use this in your next project. But wait, we are not over yet!
Finding the controls on PreviousPage with FindControl method and type-casting them from object to their real type is a little messy.
It feels like there must be a better solution for this!
And here it is: We can use the <%@ PreviousPageType %> directive in the header of our DestinationPage.aspx like this
<%@PreviousPageTypeVirtualPath=”~/SourcePage.aspx”%>
to declare our previous page type, and then we can access Public properties of the PreviousPage without typecasting.
Now all we need to do is to create some public properties on our SourcePage.aspx.cs to expose data/Controls we want to the destionation page:
publicpartialclassSourcePage : System.Web.UI.Page
{
publicstring FormFirstName
{
get { return FirstName.Text; }
}
publicstring FormLastName
{
get { return LastName.Text; }
}
}
And then we can change the Page_Load code in our DestinationPage.aspx to much cleaner code like this:
protectedvoid Page_Load(object sender, EventArgs e)
{
// first check if we had a cross page postback
if ( (PreviousPage != null) && (PreviousPage.IsCrossPagePostBack) )
{
SourcePage prevPage = PreviousPage;
// we can now use the values from textboxes and display them in two Label controls..
labelFirstName.Text = prevPage.FormFirstName;
labelLastName.Text = prevPage.FormLastName;
}
}
SourcePage type used in the code is offcourse name of the partial class defined is SourcePage.aspx.cs that inherits System.Web.UI.Page that is automatically created for us when we created new WebForm in VisualStudio.
This code is much cleaner and easier to follow, there is no ugly typecasting, just simple property values to use to retrieve the data from previous page.
Abstract class is a class which can’t be instantiated and which can have methods with definition as well as declaration also. This can be inherited.
As for Example:
public abstract class AbstractStudent
{
String Roll
{
get;
set;
}
String FirstName
{
get;
set;
}
String LastName
{
get;
set;
}
Public String GetStudentDetails()
{
// Implementation of Method
}
public String SaveStudentDetails ()
{
// Implementation of Method
}
public abstract String CalculateWage();
}
Interface can only contain the methods declaration and can be implemented in the class.
As for Example:
Public interface IStudnet
{
String Roll
{
get;
set;
}
String FirstName
{
get;
set;
}
String LastName
{
get;
set;
}
String GetStudentDetails();
String SaveStudentDetails ();
}
Below are the few main difference between Abstract Class and Interface
a. In abstract class method can have definition as well as declaration also. But Interface should have only definition.
b. All the Methods are Public as default and don’t have any access Modifier Controls in interface, whereas for abstract class we can have access modifier for methods.
c. Abstract class can have constructor or destructor, whereas interface not.
d. Abstract class can’t be part of multiple inheritance and we can implement multiple interface.
What do you mean by String objects are immutable?
String objects are immutable as its state cannot be modified once created. Every time when we perform any operation like add, copy, replace, and case conversion or when we pass a string object as a parameter to a method a new object will be created.
Example:
String str = “ABC”;
str.Replace(“A”,”X”);
Here Replace() method will not change data that “str” contains, instead a new string object is created to hold data “XBC” and the reference to this object is returned by Replace() method.
So in order to point str to this object we need to write below line.
str = str.Replace(“A”,”X”);
Now the new object is assigned to the variable str. earlier object that was assigned to str will take care by garbage collector as this one is no longer in used.
What is dll hell problem in .NET and how it will solve?
Ans: Dll hell, is kind of conflict that occurred previously, due to the lack of version supportability of dll for (within) an application
.NET Framework provides operating system with a global assembly cache. This cache is a repository for all the .net components that are shared globally on a particular machine. When a .net component installed onto the machine, the global assembly cache looks at its version, its public key and its language information and creates a strong name for the component. The component is then registered in the repository and indexed by its strong name, so there is no confusion between the different versions of same component, or DLL
What is a Partial class?
Ans: Instead of defining an entire class, you can split the definition into multiple classes by using partial class keyword. When the application compiled, c# compiler will group all the partial classes together and treat them as a single class. There are a couple of good reasons to use partial classes. Programmers can work on different parts of classes without needing to share same physical file
Ex:
Public partial class employee
{
Public void somefunction()
{
}
}
Public partial class employee
{
Public void function ()
{
}
}
What is difference between constants, read-only and, static?
Constants: The value can’t be changed
Read-only: The value will be initialized only once from the constructor of the class.
Static: Value can be initialized once.
What is the cross page post backing?
Asp.Net 2.0 fixed this with built-in features that allowed us to easily send information from one page to another.
Button control has property PostBackUrl that can be set to URL of any page in our ASP.Net WebSite where we want to transfer all form values to.
Along with that Asp.Net 2.0 Page class has a property PreviousPage that allows us to get reference to the Page object that initiated the postback (in other words to get the actual reference to the Page object of the aspx page on which user clicked the Submit button on a HTML form).
So for example lets create two sample pages in our Web Application:
- SourcePage.aspx
- DestinationPage.aspx
SourcePage.aspx:
<formid=”form1″runat=”server”>
<div>
First Name: <asp:TextBoxID=”FirstName”runat=”server”></asp:TextBox><br />
Last Name: <asp:TextBoxID=”LastName”runat=”server”></asp:TextBox><br /><br />
<asp:ButtonID=”Button1″runat=”server”Text=”Submit To Destination Page”PostBackUrl=”~/CrossPagePostbacks/DestinationPage.aspx” />
</div>
</form>
When our user clicks the Submit button, all the values from the HTML Form on SourcePage.aspx will be transfered to the DestinationPage.aspx and we will also be able to get reference to the SourcePage.aspx in our DestinationPage with the PreviousPage property like this:
So in our DestinationPage.aspx.cs code-behind we can easily access two TextBox controls on SourcePage.aspx and show them in two label controls like this:
protectedvoid Page_Load(object sender, EventArgs e)
{
// first check if we had a cross page postback
if ( (PreviousPage != null) && (PreviousPage.IsCrossPagePostBack) )
{
Page previousPage = PreviousPage;
TextBox firstName = (TextBox)previousPage.FindControl(“FirstName”);
TextBox lastName = (TextBox)previousPage.FindControl(“LastName”);
// we can now use the values from TextBoxes and display them in two Label controls..
labelFirstName.Text = firstName.Text;
labelLastName.Text = lastName.Text;
}
}
You probably noticed that we first checked if PreviousPage property of current page (DestinationPage.aspx) is NOT NULL, this is done to avoid running our code in case that user opens our DestinationPage.aspx directly, without running a cross page postback.
Also here we checked the another PreviousPage property called IsCrossPagePostBack to see if we really had a CrossPagePostback.
(If Server.Transfer is used to redirect to this page, IsCrossPagePostBack property will be set to FALSE.
TIP: We can be completely sure that we have a real CrossPagePostback ONLY IF:
- Page.PreviousPage is NOT NULL,
- PreviousPage.IsCrossPagePostback is true
Now this is very useful and i’m sure you are eager to use this in your next project. But wait, we are not over yet!
Finding the controls on PreviousPage with FindControl method and type-casting them from object to their real type is a little messy.
It feels like there must be a better solution for this!
And here it is: We can use the <%@ PreviousPageType %> directive in the header of our DestinationPage.aspx like this
<%@PreviousPageTypeVirtualPath=”~/SourcePage.aspx”%>
to declare our previous page type, and then we can access Public properties of the PreviousPage without typecasting.
Now all we need to do is to create some public properties on our SourcePage.aspx.cs to expose data/Controls we want to the destionation page:
publicpartialclassSourcePage : System.Web.UI.Page
{
publicstring FormFirstName
{
get { return FirstName.Text; }
}
publicstring FormLastName
{
get { return LastName.Text; }
}
}
And then we can change the Page_Load code in our DestinationPage.aspx to much cleaner code like this:
protectedvoid Page_Load(object sender, EventArgs e)
{
// first check if we had a cross page postback
if ( (PreviousPage != null) && (PreviousPage.IsCrossPagePostBack) )
{
SourcePage prevPage = PreviousPage;
// we can now use the values from textboxes and display them in two Label controls..
labelFirstName.Text = prevPage.FormFirstName;
labelLastName.Text = prevPage.FormLastName;
}
}
SourcePage type used in the code is offcourse name of the partial class defined is SourcePage.aspx.cs that inherits System.Web.UI.Page that is automatically created for us when we created new WebForm in VisualStudio.
This code is much cleaner and easier to follow, there is no ugly typecasting, just simple property values to use to retrieve the data from previous page.
No comments:
Post a Comment