Static classes are used when a class provides functionality that is not specific to any unique instance. Here are the features of static classes in C# 2.0.
Static classes can not be instantiated.
Static classes are sealed so they can not be inherited.
Only static members are allowed.
Static classes can only have static constructor to initialize static members.
Advantages
Compiler makes sure that no instance of static class is created. In previous version of C#, the constructor has to be marked private to avoid this from happening.
Also compiler makes sure that no instance members are declared within a static class.
Sample:
Public static class MyStaticClass
{
Private static int _staticVariable;
Public static int staticVariable;
{
Get
{
Return _staticVariable;
}
Set
{
_staticVariable = value;
}
}
Public static void Function()
{
}
}
--------------***********************----------------------------------------
A static class is defined as a class that contains only static members (of course besides the instance members inherited from System.Object and possibly a private constructor). Some languages provide built-in support for static classes. In C# 2.0, when a class is declared to be static, it is sealed, abstract, and no instance members can be overridden or declared.
public static class File {
...
}
If your language does not have built-in support for static classes, you can declare such classes manually as in the following C++ example:
public class File abstract sealed {
...
}
Static classes are a compromise between pure object-oriented design and simplicity. They are commonly used to provide shortcuts to other operations (such as System.IO.File), or functionality for which a full object-oriented wrapper is unwarranted (such as System.Environment).
DO use static classes sparingly.
Static classes should be used only as supporting classes for the object-oriented core of the framework.
DO NOT treat static classes as a miscellaneous bucket.
There should be a clear charter for the class.
DO NOT declare or override instance members in static classes.
DO declare static classes as sealed, abstract, and add a private instance constructor, if your programming language does not have built-in support for static classes.
Example of Static Class in C#.net
using System;
static class MathFunction {
// Return the reciprocal of a value.
static public double reciprocal(double num) {
return 1/num;
}
// Return the fractional part of a value.
static public double fracPart(double num) {
return num - (int) num;
}
// Return true if num is even.
static public bool isEven(double num) {
return (num % 2) == 0 ? true : false;
}
// Return true of num is odd.
static public bool isOdd(double num) {
return !isEven(num);
}
}
class MainClass {
public static void Main() {
Console.WriteLine("Reciprocal of 5 is " +
MathFunction.reciprocal(5.0));
Console.WriteLine("Fractional part of 4.234 is " +
MathFunction.fracPart(4.234));
if(MathFunction.isEven(10))
Console.WriteLine("10 is even.");
if(MathFunction.isOdd(5))
Console.WriteLine("5 is odd.");
// The following attempt to create an instance of
// MathFunction will cause an error.
// MathFunction ob = new MathFunction(); // Wrong!
}
}
Static Class
Static Class
Static classes are used when a class provides functionality that is not specific to any unique instance. Here are the features of static classes in C# 2.0.
Static classes can not be instantiated.
Static classes are sealed so they can not be inherited.
Only static members are allowed.
Static classes can only have static constructor to initialize static members.
Advantages
Compiler makes sure that no instance of static class is created. In previous version of C#, the constructor has to be marked private to avoid this from happening.
Also compiler makes sure that no instance members are declared within a static class.
Sample:
Public static class MyStaticClass
{
Private static int _staticVariable;
Public static int staticVariable;
{
Get
{
Return _staticVariable;
}
Set
{
_staticVariable = value;
}
}
Public static void Function()
{
}
}
--------------***********************----------------------------------------
A static class is defined as a class that contains only static members (of course besides the instance members inherited from System.Object and possibly a private constructor). Some languages provide built-in support for static classes. In C# 2.0, when a class is declared to be static, it is sealed, abstract, and no instance members can be overridden or declared.
public static class File {
...
}
If your language does not have built-in support for static classes, you can declare such classes manually as in the following C++ example:
public class File abstract sealed {
...
}
Static classes are a compromise between pure object-oriented design and simplicity. They are commonly used to provide shortcuts to other operations (such as System.IO.File), or functionality for which a full object-oriented wrapper is unwarranted (such as System.Environment).
DO use static classes sparingly.
Static classes should be used only as supporting classes for the object-oriented core of the framework.
DO NOT treat static classes as a miscellaneous bucket.
There should be a clear charter for the class.
DO NOT declare or override instance members in static classes.
DO declare static classes as sealed, abstract, and add a private instance constructor, if your programming language does not have built-in support for static classes.
Example of Static Class in C#.net
using System;
static class MathFunction {
// Return the reciprocal of a value.
static public double reciprocal(double num) {
return 1/num;
}
// Return the fractional part of a value.
static public double fracPart(double num) {
return num - (int) num;
}
// Return true if num is even.
static public bool isEven(double num) {
return (num % 2) == 0 ? true : false;
}
// Return true of num is odd.
static public bool isOdd(double num) {
return !isEven(num);
}
}
class MainClass {
public static void Main() {
Console.WriteLine("Reciprocal of 5 is " +
MathFunction.reciprocal(5.0));
Console.WriteLine("Fractional part of 4.234 is " +
MathFunction.fracPart(4.234));
if(MathFunction.isEven(10))
Console.WriteLine("10 is even.");
if(MathFunction.isOdd(5))
Console.WriteLine("5 is odd.");
// The following attempt to create an instance of
// MathFunction will cause an error.
// MathFunction ob = new MathFunction(); // Wrong!
}
}
Difference between Function Overriding and Function Overloading
Difference between Function Overriding and Function Overloading
Overriding is the example of run-time polymorphism and
Overloading is the example of compile-time polymorphism.
The Compile-Time polymorphism have early binding and
Runtime polymorphism have late binding.
The Compile-Time polymorphism is Static and
Runtime polymorphism is Dynamic.
Overriding
¦The return type must exactly match that of the overridden method (Note:- in some languages we can change Return type in overriding).
¦The access level must not be more restrictive than that of the overridden method.
¦The access level can be less restrictive than that of the overridden method.
¦The overriding method must not throw new or broader checked exceptions than those declared by the overridden method.
¦The overriding method can throw narrower or fewer exceptions. Just because an overridden method “takes risks” doesn’t mean that the overriding subclass’ exception takes the same risks. Bottom line: An overriding method doesn’t have to declare any exceptions that it will never throw, regardless of what the overridden method declares.
¦You cannot override a method marked final.
¦Overriding is only possible through inheritance.
¦If a method can’t be inherited, you cannot override it.
Overloaded method
¦Overloaded methods must change the argument list (you can do change in argument list by 1. changing datatype of parameters, 2. changing sequence of parameters, 3. changing count of parameters).
¦Overloaded methods can change the return type (mins its doesn't make any difference that either you change return type or not. its not matter for overloading)
¦Overloaded methods can change the access modifier.
¦Overloaded methods can declare new or broader checked exceptions.
¦A method can be overloaded in the same class or in a subclass.
NOTE:- all most all object oriented languages supports function overloading and function overriding and some of languages also supports operator overloading like : c#.net , c++ etc and some languages not support operator overloading like: vb.net but no language support operator overriding.
Labels: ASP.NET , ASP.NET Interview Questions , C#.NET and VB.NET Interview Questions , Interview Questions
Difference between Function Overriding and Function Overloading
Difference between Function Overriding and Function Overloading
Overriding is the example of run-time polymorphism and
Overloading is the example of compile-time polymorphism.
The Compile-Time polymorphism have early binding and
Runtime polymorphism have late binding.
The Compile-Time polymorphism is Static and
Runtime polymorphism is Dynamic.
Overriding
¦The return type must exactly match that of the overridden method (Note:- in some languages we can change Return type in overriding).
¦The access level must not be more restrictive than that of the overridden method.
¦The access level can be less restrictive than that of the overridden method.
¦The overriding method must not throw new or broader checked exceptions than those declared by the overridden method.
¦The overriding method can throw narrower or fewer exceptions. Just because an overridden method “takes risks” doesn’t mean that the overriding subclass’ exception takes the same risks. Bottom line: An overriding method doesn’t have to declare any exceptions that it will never throw, regardless of what the overridden method declares.
¦You cannot override a method marked final.
¦Overriding is only possible through inheritance.
¦If a method can’t be inherited, you cannot override it.
Overloaded method
¦Overloaded methods must change the argument list (you can do change in argument list by 1. changing datatype of parameters, 2. changing sequence of parameters, 3. changing count of parameters).
¦Overloaded methods can change the return type (mins its doesn't make any difference that either you change return type or not. its not matter for overloading)
¦Overloaded methods can change the access modifier.
¦Overloaded methods can declare new or broader checked exceptions.
¦A method can be overloaded in the same class or in a subclass.
NOTE:- all most all object oriented languages supports function overloading and function overriding and some of languages also supports operator overloading like : c#.net , c++ etc and some languages not support operator overloading like: vb.net but no language support operator overriding.
Labels: ASP.NET , ASP.NET Interview Questions , C#.NET and VB.NET Interview Questions , Interview Questions
Special folders in ASP.Net
With the release of ASP.NET 2.0, Microsoft has greatly increased the power of ASP.Net by introducing a suite of new features and functionalities.
ASP.Net defines several special folders. When a new Web site is created the App_Data folder is created by default; it can contain a SQL Server 2005 Express Edition database, another database, or an XML data file that will be used in the Web site.
These folders are also data directories.
~:App_Browsers folder - Contains browser definitions (.browser files) files that ASP.Net uses to identify individual browsers and determine their capabilities. Browser definition files are used to determine the client browser capabilities. These files are often used to help support mobile application.
~:App_Data folder - App_Data folder is used to store file that can be used as database files (.mdf, .mdb and xml files). The user account that is used to run the application (for example, the local ASPNET account) has permissions to read, write, and create files in this folder. Various ASP.NET application features, such as the providers for membership and roles, as well as the Web Site Administration Tool, are configured to work with the App_Data folder specifically.
~:Bin folder - Contains compiled assemblies (.dll files), for code that you want to reference in your application, as in earlier versions of Visual Studio. Any classes represented by code in the Bin folder are automatically referenced in your Web site.
~:App_LocalResources folder - Contains .resx files that are bound to a specific page. You can define multiple .resx files for each page, each .resx file representing a different language or language/culture combination.
~:App_GlobalResource folder - Like the App_LocalResources folders, but contains .resx files that are not bound to a specific page. Resource values in .resx files in the App_GlobalResource folders can be accessed programmatically from application code.
~:App_Code folder - Contains source code files. The code is compiled as part of your application and is referenced automatically. The App_Code folder works much like the Bin folder, except that you can put source code in it instead of compiled code. While you are working in Visual Web Developer, the source code in the App_Code folder is compiled dynamically so that IntelliSense can reference any classes defined in the files.
~:App_Themes folder - Contain sub-folders that each defines a specific theme or look and feel for you Web site. Sub-folders consist of a collection of files (such as .skin, .css and image files) that define the appearance of ASP.Net Web pages and controls.
~:App_WebReferences folder - Contains files used to create a reference to a Web service (in the same project or external to the project), including .disco, .xsd, .discomap and .wsdl files.
Note: Special folders can be added to a Web site from the Visual Studio menu system. Typically this involves right-clicking the Web Application project and selecting Add ASP.NET folder.
Labels: ASP.NET , ASP.NET Interview Questions , Interview Questions
Special folders in ASP.Net
With the release of ASP.NET 2.0, Microsoft has greatly increased the power of ASP.Net by introducing a suite of new features and functionalities.
ASP.Net defines several special folders. When a new Web site is created the App_Data folder is created by default; it can contain a SQL Server 2005 Express Edition database, another database, or an XML data file that will be used in the Web site.
These folders are also data directories.
~:App_Browsers folder - Contains browser definitions (.browser files) files that ASP.Net uses to identify individual browsers and determine their capabilities. Browser definition files are used to determine the client browser capabilities. These files are often used to help support mobile application.
~:App_Data folder - App_Data folder is used to store file that can be used as database files (.mdf, .mdb and xml files). The user account that is used to run the application (for example, the local ASPNET account) has permissions to read, write, and create files in this folder. Various ASP.NET application features, such as the providers for membership and roles, as well as the Web Site Administration Tool, are configured to work with the App_Data folder specifically.
~:Bin folder - Contains compiled assemblies (.dll files), for code that you want to reference in your application, as in earlier versions of Visual Studio. Any classes represented by code in the Bin folder are automatically referenced in your Web site.
~:App_LocalResources folder - Contains .resx files that are bound to a specific page. You can define multiple .resx files for each page, each .resx file representing a different language or language/culture combination.
~:App_GlobalResource folder - Like the App_LocalResources folders, but contains .resx files that are not bound to a specific page. Resource values in .resx files in the App_GlobalResource folders can be accessed programmatically from application code.
~:App_Code folder - Contains source code files. The code is compiled as part of your application and is referenced automatically. The App_Code folder works much like the Bin folder, except that you can put source code in it instead of compiled code. While you are working in Visual Web Developer, the source code in the App_Code folder is compiled dynamically so that IntelliSense can reference any classes defined in the files.
~:App_Themes folder - Contain sub-folders that each defines a specific theme or look and feel for you Web site. Sub-folders consist of a collection of files (such as .skin, .css and image files) that define the appearance of ASP.Net Web pages and controls.
~:App_WebReferences folder - Contains files used to create a reference to a Web service (in the same project or external to the project), including .disco, .xsd, .discomap and .wsdl files.
Note: Special folders can be added to a Web site from the Visual Studio menu system. Typically this involves right-clicking the Web Application project and selecting Add ASP.NET folder.
Labels: ASP.NET , ASP.NET Interview Questions , Interview Questions
Special folders in ASP.Net
With the release of ASP.NET 2.0, Microsoft has greatly increased the power of ASP.Net by introducing a suite of new features and functionalities.
ASP.Net defines several special folders. When a new Web site is created the App_Data folder is created by default; it can contain a SQL Server 2005 Express Edition database, another database, or an XML data file that will be used in the Web site.
These folders are also data directories.
~:App_Browsers folder - Contains browser definitions (.browser files) files that ASP.Net uses to identify individual browsers and determine their capabilities. Browser definition files are used to determine the client browser capabilities. These files are often used to help support mobile application.
~:App_Data folder - App_Data folder is used to store file that can be used as database files (.mdf, .mdb and xml files). The user account that is used to run the application (for example, the local ASPNET account) has permissions to read, write, and create files in this folder. Various ASP.NET application features, such as the providers for membership and roles, as well as the Web Site Administration Tool, are configured to work with the App_Data folder specifically.
~:Bin folder - Contains compiled assemblies (.dll files), for code that you want to reference in your application, as in earlier versions of Visual Studio. Any classes represented by code in the Bin folder are automatically referenced in your Web site.
~:App_LocalResources folder - Contains .resx files that are bound to a specific page. You can define multiple .resx files for each page, each .resx file representing a different language or language/culture combination.
~:App_GlobalResource folder - Like the App_LocalResources folders, but contains .resx files that are not bound to a specific page. Resource values in .resx files in the App_GlobalResource folders can be accessed programmatically from application code.
~:App_Code folder - Contains source code files. The code is compiled as part of your application and is referenced automatically. The App_Code folder works much like the Bin folder, except that you can put source code in it instead of compiled code. While you are working in Visual Web Developer, the source code in the App_Code folder is compiled dynamically so that IntelliSense can reference any classes defined in the files.
~:App_Themes folder - Contain sub-folders that each defines a specific theme or look and feel for you Web site. Sub-folders consist of a collection of files (such as .skin, .css and image files) that define the appearance of ASP.Net Web pages and controls.
~:App_WebReferences folder - Contains files used to create a reference to a Web service (in the same project or external to the project), including .disco, .xsd, .discomap and .wsdl files.
Note: Special folders can be added to a Web site from the Visual Studio menu system. Typically this involves right-clicking the Web Application project and selecting Add ASP.NET folder.
Labels: ASP.NET , ASP.NET Interview Questions , Interview Questions
Special folders in ASP.Net
With the release of ASP.NET 2.0, Microsoft has greatly increased the power of ASP.Net by introducing a suite of new features and functionalities.
ASP.Net defines several special folders. When a new Web site is created the App_Data folder is created by default; it can contain a SQL Server 2005 Express Edition database, another database, or an XML data file that will be used in the Web site.
These folders are also data directories.
~:App_Browsers folder - Contains browser definitions (.browser files) files that ASP.Net uses to identify individual browsers and determine their capabilities. Browser definition files are used to determine the client browser capabilities. These files are often used to help support mobile application.
~:App_Data folder - App_Data folder is used to store file that can be used as database files (.mdf, .mdb and xml files). The user account that is used to run the application (for example, the local ASPNET account) has permissions to read, write, and create files in this folder. Various ASP.NET application features, such as the providers for membership and roles, as well as the Web Site Administration Tool, are configured to work with the App_Data folder specifically.
~:Bin folder - Contains compiled assemblies (.dll files), for code that you want to reference in your application, as in earlier versions of Visual Studio. Any classes represented by code in the Bin folder are automatically referenced in your Web site.
~:App_LocalResources folder - Contains .resx files that are bound to a specific page. You can define multiple .resx files for each page, each .resx file representing a different language or language/culture combination.
~:App_GlobalResource folder - Like the App_LocalResources folders, but contains .resx files that are not bound to a specific page. Resource values in .resx files in the App_GlobalResource folders can be accessed programmatically from application code.
~:App_Code folder - Contains source code files. The code is compiled as part of your application and is referenced automatically. The App_Code folder works much like the Bin folder, except that you can put source code in it instead of compiled code. While you are working in Visual Web Developer, the source code in the App_Code folder is compiled dynamically so that IntelliSense can reference any classes defined in the files.
~:App_Themes folder - Contain sub-folders that each defines a specific theme or look and feel for you Web site. Sub-folders consist of a collection of files (such as .skin, .css and image files) that define the appearance of ASP.Net Web pages and controls.
~:App_WebReferences folder - Contains files used to create a reference to a Web service (in the same project or external to the project), including .disco, .xsd, .discomap and .wsdl files.
Note: Special folders can be added to a Web site from the Visual Studio menu system. Typically this involves right-clicking the Web Application project and selecting Add ASP.NET folder.
Labels: ASP.NET , ASP.NET Interview Questions , Interview Questions
What is Code Replacement in ASP.Net
.NET Framework assemblies are typically compiled and deployed into a Windows DLL-based PE format. When the common language runtime's loader resolves a class implemented within this type of assembly, it calls the Windows LoadLibrary routine on the file (which locks its access on disk), and then maps the appropriate code data into memory for run-time execution. Once loaded, the DLL file will remain locked on disk until the application domain referencing it is either torn down or manually recycled.
Although ASP.NET cannot prevent the common language runtime from locking a loaded assembly DLL on disk, it can support you by ensuring that the physical DLLs in a Web application's private assembly cache are never actually loaded by the runtime. Instead, shadow copies of the assembly DLLs are made immediately prior to their use. These shadow assemblies--not the original files--are then locked and loaded by the runtime.
Because the original assembly files always remain unlocked, you are free to delete, replace, or rename them without cycling the Web server or having to use a registration utility. FTP and similar methods work just fine. ASP.NET maintains an active list of all assemblies loaded within a particular application's application domain and uses file-change monitoring code to watch for any updates to the original files.
Labels: ASP.NET , ASP.NET Interview Questions , Interview Questions
What is Code Replacement in ASP.Net
.NET Framework assemblies are typically compiled and deployed into a Windows DLL-based PE format. When the common language runtime's loader resolves a class implemented within this type of assembly, it calls the Windows LoadLibrary routine on the file (which locks its access on disk), and then maps the appropriate code data into memory for run-time execution. Once loaded, the DLL file will remain locked on disk until the application domain referencing it is either torn down or manually recycled.
Although ASP.NET cannot prevent the common language runtime from locking a loaded assembly DLL on disk, it can support you by ensuring that the physical DLLs in a Web application's private assembly cache are never actually loaded by the runtime. Instead, shadow copies of the assembly DLLs are made immediately prior to their use. These shadow assemblies--not the original files--are then locked and loaded by the runtime.
Because the original assembly files always remain unlocked, you are free to delete, replace, or rename them without cycling the Web server or having to use a registration utility. FTP and similar methods work just fine. ASP.NET maintains an active list of all assemblies loaded within a particular application's application domain and uses file-change monitoring code to watch for any updates to the original files.
Labels: ASP.NET , ASP.NET Interview Questions , Interview Questions
In how many ways you can create new copies of an existing string in C#?
There are two ways to create new copies of an existing string in C#:
1. Using overloaded = operator like - string s2 = s1;
2. Using the static Copy method like - string s2 = string.Copy(s1);
In how many ways you can compare two strings in C# using overloaded methods and operators?
There are three ways:
1. Overloaded Compare() method
2. Overloaded Equal() method
3. Overloaded == operator.
In how many ways you can create new copies of an existing string in C#?
There are two ways to create new copies of an existing string in C#:
1. Using overloaded = operator like - string s2 = s1;
2. Using the static Copy method like - string s2 = string.Copy(s1);
In how many ways you can compare two strings in C# using overloaded methods and operators?
There are three ways:
1. Overloaded Compare() method
2. Overloaded Equal() method
3. Overloaded == operator.