What Is Namespace?: Appdomain
What Is Namespace?: Appdomain
Namespace is a group of classes, structures, interfaces, enumerations, and delegates, organized in a logical
hierarchy by function, that enable you to access the core functionality you need in your applications.
Namespaces are the way that .NET avoids name clashes between classes. A namespace is no more than a
grouping of data types, but it has the effect that the names of all data types within a namespace automatically
get prefixed with the name of the namespace. It is also possible to nest namespaces within each other.
Each namespace in the FCL can contain multiple namespaces, or they can contain classes that expose
properties and methods that you call in your applications. The namespaces within the FCL are grouped by
the functionality they provide, which makes it very easy to find what you're looking for.
2. How does the code written in different languages get executed in .net?
The CLR is language-neutral, which means that it can run code written in C#, Visual Basic.NET, Visual C+
+, Visual J#, Net COBOL, RPG.NET, or any other language, as long as that language is first compiled to the
bytestream format the CLR expects. When compiling to managed code, the language-specific compiler
translates the source code into Microsoft Intermediate Language (MSIL), which is a CPU-independent set of
instructions that can be efficiently converted to native code. MSIL includes instructions for loading, storing,
initializing, and calling methods on objects, as well as instructions for arithmetic and logical operations,
control flow, direct memory access, exception handling, and other operations. The compiled MSIL is placed
into an EXE or DLL file, called a Portable Executable (PE) file.
Before code can be run, the MSIL in the PE must be converted to CPU-specific code, by a CPU-specific
just-in-time (JIT) compiler. Because the Common Language Runtime supplies one or more JIT compilers
for each computer-processor instruction architecture it supports, the same set of MSIL can be JIT-compiled
and run on any supported architecture for example, Itanium, x64, or x86. During execution, managed code
receives services such as garbage collection, security, interoperability with unmanaged code, cross-language
debugging support, and enhanced deployment and versioning support from the CLR .
For example, it is possible to use any user defined type, e.g. TeacherClass, as a parameter in calls to these
general purpose classes. However, the general purpose data structures cannot be sure that the type passed in
is what is expected. If it is not, it frequently results in ugly runtime errors in the application. Also, if the type
is returned from the method call after processing, the result must explicitly be cast back to the appropriate
type, which adds further overhead. Generics provide a way to create strongly typed parameters, allowing
reuse of the general purpose routine without the associated type safety issues and the overhead of run-time
type checking. Generics are available to any .NET language in the .NET Framework 2.0.
Establishes the assembly identity (in the form of a text name), version, culture, and digital signature (if the
assembly is to be shared across applications).
• Defines what files (by name and file hash) make up the assembly implementation.
• Specifies the types and resources that make up the assembly, including which are exported from the
assembly.
• Itemizes the compile-time dependencies on other assemblies.
• Specifies the set of permissions required for the assembly to run properly.
• This information is used at run time to resolve references, enforce version binding policy, and
validate the integrity of loaded assemblies. The runtime can determine and locate the assembly for
any running object, since every type is loaded in the context of an assembly. Assemblies are also the
unit at which code access security permissions are applied. The identity evidence for each assembly
is considered separately when determining what permissions to grant the code it contains.
The self-describing nature of assemblies also helps makes zero-impact install and XCOPY deployment
feasible.
Assemblies deployed in the global assembly cache must have a strong name. When an assembly is added to
the global assembly cache, integrity checks are performed on all files that make up the assembly.
12. What are the ways to deploy an assembly into the (GAC) global assembly cache?
There are several ways to deploy an assembly into the global assembly cache:
• Use an installer designed to work with the global assembly cache. This is the preferred option for
installing assemblies into the global assembly cache.
• Use a developer tool called the Global Assembly Cache tool (Gacutil.exe), provided by the .NET
Framework SDK.
• Use Windows Explorer to drag assemblies into the cache.
14. What is the difference between a private assembly and a shared assembly?
A private assembly is normally used by a single application, and is stored in the application's directory, or a
sub-directory beneath.
A shared assembly is intended to be used by multiple applications, and is normally stored in the global
assembly cache (GAC), which is a central repository for assemblies.
A shared assembly can also be stored outside the GAC, in which case each application must be pointed to
its location via a codebase entry in the application's configuration file. The main advantage of deploying
assemblies to the GAC is that the GAC can support multiple versions of the same assembly side-by-side.
15. What is the difference between struct and class in C#?
Structs vs classes in C#
Structs may seem similar to classes, but there are important differences that you should be aware of. First of
all, classes are reference types and structs are value types. By using structs, you can create objects that
behave like the built-in types and enjoy their benefits as well.
• When you call the New operator on a class, it will be allocated on the heap. However, when you
instantiate a struct, it gets created on the stack. This will yield performance gains. Also, you will not
be dealing with references to an instance of a struct as you would with classes. You will be working
directly with the struct instance. Because of this, when passing a struct to a method, it's passed by
value instead of as a reference.
• Structs can declare constructors, but they must take parameters. It is an error to declare a default
(parameterless) constructor for a struct. Struct members cannot have initializers. A default
constructor is always provided to initialize the struct members to their default values.
• When you create a struct object using the New operator, it gets created and the appropriate
constructor is called. Unlike classes, structs can be instantiated without using the New operator. If
you do not use New, the fields will remain unassigned and the object cannot be used until all the
fields are initialized.
• There is no inheritance for structs as there is for classes. A struct cannot inherit from another struct
or class, and it cannot be the base of a class. Structs, however, inherit from the base class object. A
struct can implement interfaces, and it does that exactly as classes do,
• Structs are simple to use and can prove to be useful at times. Just keep in mind that they're created on
the stack and that you're not dealing with references to them but dealing directly with them.
Whenever you have a need for a type that will be used often and is mostly just a piece of data, structs
might be a good option.
18. Which interface(s) must a class implement in order to support the foreach statement?
Required interface for foreach statement:
A class must implement the IEnumerable and IEnumerator interfaces to support the foreach statement.
...
Now, if you use the new keyword instead of override, the method in the derived class doesn't override the
method in the base class, it merely hides it. In that case, code like this:
public class Base
{
public virtual void SomeOtherMethod()
{
}
}
public class Derived : Base
{
public new void SomeOtherMethod()
{
}
}
...
21. Name two ways that you can prevent a class from being instantiated.
Ways to prevent a class from instantiated:
A class cannot be instantiated if it is abstract or if it has a private constructor.
Sample code:
// Namespace:
using act = System.Runtime.Remoting.Activation;
// Class:
using list = System.Collections.ArrayList;
...
list l = new list(); // Creates an ArrayList
act.UrlAttribute obj; // Equivalent to System.Runtime.Remoting.Activation.UrlAttribute obj
26. What keyword must a derived class use to replace a non-virtual inherited method?
new keyword is used to replace (not override) an inherited method with one of the same name.
To know the difference between new and override read: new vs override in C#
In the customErrors element, specify a default redirect page. There are three modes for the default redirect
page
On
Unhandled exceptions will redirect the user to the specified defaultredirect page. This is used mainly in
production.
Off
Users will see the exception information and not be redirected to the defaultredirect page. This is used
mainly in development.
RemoteOnly
Only users accessing the site on the local machine (using localhost) will see the exception information while
all other users will be redirected to the defaultredirect page. This is used mainly for debugging.
In addition to the default redirect page, you can set specific pages for certain HTTP error codes. For
example, you can specify that all 404 errors result in a certain error page, while all 500 errors result in
another
28. Why should you avoid the excessive use of ViewState in Asp.Net web page?
Automatic state management is a feature that enables server controls to re-populate their values on a round
trip without requiring you to write any code. This feature is not free however, since the state of a control is
passed to and from the server in a hidden form field.
Since the view state data resides in a hidden form field (__VIEWSTATE); ViewState increases the size of
page and results in slow loading of page at browser. You should be aware of when ViewState is helping you
and when it is not.
For example, if you are binding a control to data on every round trip , then you do not need the control to
maintain it's view state, since you will wipe out any re-populated data in any case. ViewState is enabled for
all server controls by default. To disable it, set the EnableViewState property of the control to false, as in
the following example:
< asp:datagrid EnableViewState="false" datasource="..." runat="server"/>
You can also turn ViewState off at the page level. This is useful when you do not post back from a page at
all, as in the following example:
< %@ Page EnableViewState="false" %>
Note that this attribute is also supported by the User Control directive.
33. What method do you use to explicitly kill a users session in ASP.NET?
The Session.Abandon method destroys all the objects stored in a Session object and releases their
resources. If you do not call the Abandon method explicitly, the server destroys these objects when the
session times out.
34. What does the "EnableViewState" property do? Why would I want it on or off?
EnableViewState turns on the automatic state management feature that enables server controls to re-populate
their values on a round trip without requiring you to write any code. This feature is not free however, since
the state of a control is passed to and from the server in a hidden form field. You should be aware of when
ViewState is helping you and when it is not.
For example, if you are binding a control to data on every round, then you do not need the control to
maintain its view state, since you will wipe out any re-populated data in any case. ViewState is enabled for
all server controls by default. To disable it, set the EnableViewState property of the control to false.
35. What are the different modes for the sessionstates in the web.config file?
Off: Indicates that session state is not enabled.
Inproc: Indicates that session state is stored locally.
StateServer: Indicates that session state is stored on a remote server.
SQLServer: Indicates that session state is stored on the SQL Server.
42. Explain the Limitations & Issues with Automatic SQL Server State Management?
• Although using SQL Server to store your session state relieves you of many difficult development
issues, you’ll still need to consider some important limitations:
• You’re limited to SQL Server.
• This technique can only use SQL Server, no other server database. If you do not have a SQL Server
installation available, you will be unable to use this solution.
• Performance may suffer.
• Like any of the state management techniques, using SQL Server to manage your applications state
can cause your performance degrade a little. Because it takes a little bit of time to make a connection
and read and write state information in the database, theres no avoiding a small bit of overhead.
If the root project directory is MyProject and the aspx file is located at root then to get the same path use
code
/* physical path of TextFiles */
string TextFilePath=Server.MapPath("Files/TextFiles");
The first two properties are used to document a web method, while the others affect its behavior.
Adapters are used to exchange data between a data source and a dataset. In many applications, this means
reading data from a database into a dataset, and then writing changed data from the dataset back to the
database. You can use a data adapter to perform the following operations:
• Retrieve rows from a data store into corresponding data tables within the dataset.
• To retrieve rows into a dataset, use the Fill method on a data adapter object (SqlDataAdapter,
OleDbDataAdapter, OdbcDataAdapter, or OracleDataAdapter). When you invoke the Fill method, it
transmits an SQL SELECT statement to the data store.
• Transmit changes made to a dataset table to the corresponding data store.
• To transmit a dataset table of the dataset to the data store, use the adapter's Update method. When
you invoke the method, it executes whatever SQL INSERT, UPDATE or DELETE statements are
needed, depending on whether the affected record is new, changed, or deleted.
60. Which DataAdapters are available for use with different Databases in ADO.NET?
ADO.NET makes these data adapters available for use with databases:
• The OleDbDataAdapter object is suitable for use with any data source exposed by an OLE DB
provider.
• The SqlDataAdapter object is specific to SQL Server. Because it does not have to go through an
OLE DB layer, it is faster than the OleDbDataAdapter. However, it can only be used with SQL
Server 7.0 or later.
• The OdbcDataAdapter object is optimized for accessing ODBC data sources.
• The OracleDataAdapter object is optimized for accessing Oracle databases.
61. How can you update Database using DataAdapter and the DataSet?
Updating the Database with a DataAdapter and the DataSet
The Update method of the DataAdapter is called to resolve changes from a DataSet back to the data source.
The Update method, like the Fill method, takes as arguments an instance of a DataSet, and an optional
DataTable object or DataTable name. The DataSet instance is the DataSet that contains the changes that
have been made, and the DataTable identifies the table from which to retrieve the changes.
When you call the Update method, the DataAdapter analyzes the changes that have been made and executes
the appropriate command (INSERT, UPDATE, or DELETE). When the DataAdapter encounters a change to
a DataRow, it uses the InsertCommand, UpdateCommand, or DeleteCommand to process the change.
The Update method will resolve your changes back to the data source, however other clients may have
modified data at the data source since the last time you filled the DataSet. To refresh your DataSet with
current data, use the DataAdapter and Fill the DataSet again. New rows will be added to the table, and
updated information will be incorporated into existing rows. The Fill method determines whether a new row
will be added or an existing row will be updated by examining the primary key values of the rows in the
DataSet and the rows returned by the SelectCommand. If the Fill method encounters a primary key value for
a row in the DataSet that matches a primary key value from a row in the results returned by the
SelectCommand, it updates the existing row with the information from the row returned by the
SelectCommand and sets the RowState of the existing row to Unchanged. If a row returned by the
SelectCommand has a primary key value that does not match any of the primary key values of the rows in
the DataSet, the Fill method adds a new row with a RowState of Unchanged.
txtName.Focus();
You can also set the focus of a control through the SetFocus( ) method of the Page class. The syntax is:
Page.SetFocus(controlName);
Apart from this you can use defaultfocus attribute to set the control that will be assigned the focus when
the form is loaded.
< form id="form1" runat="server"
defaultfocus="txtEmail"
>
For example, you have a page with two TextBox controls and two Button controls. You can configure the
Submit button to be the default button so it is automatically clicked when a user presses the Enter key. To
set the default button of form use
< form id="form1" runat="server"
defaultbutton="btnSubmit">
83. What is the difference between value parameter and reference parameter?
A value parameter is used for "in" parameter passing, in which the value of an argument is passed into a
method, and modifications of the parameter do not impact the original argument. A value parameter refers to
its own variable, one that is distinct from the corresponding argument. This variable is initialized by copying
the value of the corresponding argument.
A reference parameter is used for "by reference" parameter passing, in which the parameter acts as an alias
for a caller-provided argument. A reference parameter does not itself define a variable, but rather refers to
the variable of the corresponding argument. Modifications of a reference parameter impact the
corresponding argument.
85. What is the difference between reference parameter and output parameter?
Modifications of a reference parameter and output parameter impact the corresponding argument. So an
output parameter is similar to a reference parameter, except that the initial value of the caller-provided
argument is unimportant.
In C# a reference parameter is declared with a ref modifier but an output parameter is declared with an out
modifier.
Constants are permitted to depend on other constants within the same program as long as there are no
circular dependencies. The example
class Constants
{
public const int A = 1;
public const int B = A + 1;
}
shows a class named Constants that has two public constants