Yes, we can validate a DropDownList by RequiredFieldValidator. To perform this validation, we have to set the InitialValue property of RequiredFieldValidator control.
Here I am posting the important questions whatever I have faced in my interviews. (Few of the contents & links have been taken from various blogs/articles.) Hope it will help you..
Friday, 23 May 2014
What is the difference between the Response.Write() and Response.Output.Write() methods?
The Response.Write() method allows you to write the normal output; whereas, the Response.Output.Write() method allows you to write the formatted output.
How to implement singleton design pattern in C#?
In singleton pattern, a class can only have one instance and provides access point to it globally.
Eg:
[csharp]
Public sealed class Singleton
{
Private static readonly Singleton _instance = new Singleton();
}
[/csharp]
Eg:
[csharp]
Public sealed class Singleton
{
Private static readonly Singleton _instance = new Singleton();
}
[/csharp]
Why can’t you specify the accessibility modifier for methods inside the interface?
In an interface, we have virtual methods that do not have method definition. All the methods are there to be overridden in the derived class. That’s why they all are public.
What are delegates?
Delegates are same are function pointers in C++ but the only difference is that they are type safe unlike function pointers. Delegates are required because they can be used to write much more generic type safe functions.
List down the commonly used types of exceptions in .Net?
- ArgumentException
- ArgumentNullException
- ArgumentOutOfRangeException
- ArithmeticException
- DivideByZeroException
- OverflowException
- IndexOutOfRangeException
- InvalidCastException
- InvalidOperationException
- IOEndOfStreamException
- NullReferenceException
- OutOfMemoryException
- StackOverflowException etc.
What are generics in C#.NET?
Generics are used to make reusable code classes to decrease the code redundancy, increase type safety and performance. Using generics, we can create collection classes. To create generic collection, System.Collections.Generic namespace should be used instead of classes such as ArrayList in the System.Collections namespace. Generics promotes the usage of parameterized types.
What’s the difference between the System.Array.CopyTo() and System.Array.Clone() ?
Using Clone() method, we creates a new array object containing all the elements in the original array and using CopyTo() method, all the elements of existing array copies into another existing array. Both the methods perform a shallow copy.
What are the differences between System.String and System.Text.StringBuilder classes?
System.String is immutable. When we modify the value of a string variable then a new memory is allocated to the new value and the previous memory allocation released. System.StringBuilder was designed to have concept of a mutable string where a variety of operations can be performed without allocation separate memory location for the modified string.
What are Custom Control and User Control?
Custom Controls are controls generated as compiled code (Dlls), those are easier to use and can be added to toolbox. Developers can drag and drop controls to their web forms. Attributes can be set at design time. We can easily add custom controls to Multiple Applications (If Shared Dlls), If they are private then we can copy to dll to bin directory of web application and then add reference and can use them.
User Controls are very much similar to ASP include files, and are easy to create. User controls can’t be placed in the toolbox and dragged – dropped from it. They have their design and code behind. The file extension for user controls is ascx.
User Controls are very much similar to ASP include files, and are easy to create. User controls can’t be placed in the toolbox and dragged – dropped from it. They have their design and code behind. The file extension for user controls is ascx.
What is difference between constants and read-only?
Constant variables are declared and initialized at compile time. The value can’t be changed after wards. Read-only variables will be initialized only from the Static constructor of the class. Read only is used only when we want to assign the value at run time.
What is Classes and Objects in C# .NET ?
Objects are created from Classes, The Windows Form itself is a Class, and when you run your program, you are creating Objects: a Form object, a button object, a Textbox object, etc.
An object is an instance of a class through which we access the methods of that class. “New” keyword is used to create an object. A class that creates an object in memory will contain the information about the methods, variables and behavior of that class.
Example:
Reference:
http://www.c-sharpcorner.com/UploadFile/84c85b/object-oriented-programming-using-C-Sharp-net/
Class:
A Class is simply a block of code that does a particular job. You might have a class that handles all your database work, for example, or one that does error checking on Textboxes.
The idea is that you can reuse this code (Class) whenever you need it, or when you need it in other projects. It saves you from having to write the same thing over and over again.
Think of a Class as a recipe. If you had a recipe for a chicken biryani, the recipe will tell you what you need to do to make the biryani. But it's not the biryani itself. It's the instructions for the biryani. If you have the recipe, you can make chicken biryani whenever you need them.
Object:
An Object is the thing that the recipe makes - The Chicken biryani itself. You do all the coding in a Class (recipe), and then instruct C# to make an object (Chicken).
But all you are really doing with Classes and Objects is trying to separate code into blocks, so that it can be reused. When the code is being used, it's an object. But the object is created from your code (Class).
Example:
Reference:
http://www.c-sharpcorner.com/UploadFile/84c85b/object-oriented-programming-using-C-Sharp-net/
What is the difference between public, static and void?
All these are access modifiers in C#. Public declared variables or methods are accessible anywhere in the application. Static declared variables or methods are globally accessible without creating an instance of the class. The compiler stores the address of the method as the entry point and uses this information to begin execution before any objects are created. And Void is a type modifier that states that the method or variable does not return any value.
Can multiple catch blocks be executed?
No, Multiple catch blocks can’t be executed. Once the proper catch code executed, the control is transferred to the finally block and then the code that follows the finally block gets executed.
What is connection pooling and how do you make your application use it?
- Connection pool is the collection of new, open and all type of database connections.
- Opening database connection is a time consuming operation.
- Connection pooling increases the performance of the applications by reusing the active database connections instead of creating new connection for every request.
- Connection pooling behaviour is controlled by the connection string parameters.
Following are the 4 parameters that control most of the connection pooling behaviour:
- Connect Timeout
- Max Pool Size
- Min Pool Size
- Pooling
How does output caching work in ASP.NET?
Output caching is a powerful technique that increases request/response throughput by caching the content generated from dynamic pages. Output caching is enabled by default, but output from any given response is not cached unless explicit action is taken to make the response cacheable.
To make a response eligible for output caching, it must have a valid expiration/validation policy and public cache visibility. This can be done using either the low-level
OutputCache
API or the high-level @ OutputCache
directive. When output caching is enabled, an output cache entry is created on the first GET
request to the page. SubsequentGET
or HEAD
requests are served from the output cache entry until the cached request expires.
The output cache also supports variations of cached
GET
or POST
name/value pairs.
The output cache respects the expiration and validation policies for pages. If a page is in the output cache and has been marked with an expiration policy that indicates that the page expires 60 minutes from the time it is cached, the page is removed from the output cache after 60 minutes. If another request is received after that time, the page code is executed and the page can be cached again. This type of expiration policy is called absolute expiration - a page is valid until a certain time.
What is an Interface?
An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to
Abstract
class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract
class. Since C# doesn’t support multiple inheritance, interfaces are used to implement multiple inheritance.What is the use/significance of Dispose and Finalize method?
.NET Framework provides two methods Finalize and Dispose for
releasing unmanaged resources like: Windows API created objects, File,
Database connection objects, COM objects.
It is always recommended to use Dispose method to clean
unmanaged resources. Do not implement the Finalize method until it is extremely
necessary
Dispose:
Dispose method belongs to ‘IDisposable’ interface. If any
object wants to release its unmanaged code, the best is to implement IDisposable and override
the Dispose method of IDisposable interface. Now once
your class has exposed the Dispose method, it is
the responsibility of the client to call the Dispose method to do the
cleanup.
How do I force the Dispose method to be called
automatically, as clients can forget to call Dispose method?
Call the Dispose method in Finalize method and in Dispose method, suppress
the finalize method using GC.SuppressFinalize.
Below is the sample code of the pattern. This is the best way we
do clean our unallocated resources and yes not to forget we do not get the hit
of running the Garbage collector twice.
public class CleanClass :
IDisposable
{
public void Dispose()
{
GC.SuppressFinalize(this);
}
~CleanClass()
{
Dispose();
}
}
Finalize:
.NET Garbage collector does almost all clean up activity for your objects. But unmanaged resources (example: Windows API created objects, File, Database connection objects, COM objects, etc.) are outside the scope of .NET Framework. We have to explicitly clean our resources. For these types of objects, .NET Framework providesObject.Finalize method.
What is the difference between Finalize() and Dispose() methods?
Dispose:
- Dispose
It belongs to IDisposable interface. and internal called by user code.
- Dispose
() of IDisposable interface is called by the programmer to explicitly
release resources when they are no longer being used. Dispose () can be
called even if other references to the object are alive.
- It
is called by user code and the class implementing dispose method must
implement IDisposable interface.It belongs to IDisposable
interface.Implement this when you are writing a custom class that will be
used by other users.There is no performance costs associated with Dispose
method
Finalize:
- Finalize
It belongs to Object class. and It's implemented with the help of
destructor in C#.
- Used
to free unmanaged resources like files, database connections, COM etc.
held by an object before that object is destroyed.Internally, it is called
by Garbage Collector and cannot be called by user code.
- The
finalizer method is called when your object is garbage collected and you
have no guarantee when this will happen (you can force it, but it will
hurt performance).
Why is it preferred to not use finalize for clean up?
The problem with finalize is that garbage collection has to make two rounds in order to remove objects which have finalize methods.
Let us assume, there are three objects, Object1, Object2, and Object3.
Object2 has the finalize method overridden and remaining objects do not have the finalize method overridden.
Now when garbage collector runs for the first time, it searches
for objects whose memory has to free. It can see three objects but only cleans
the memory for Object1 and Object3.
Object2 it pushes to the finalization queue.
Now garbage collector runs for the second time. It sees there
are no objects to be released and then checks for the finalization queue and at
this moment, it clears object2 from the memory.
So if you notice, object2 was released from memory in the second round and not first. That is why the best practice is not to write clean up non.NET/unmanaged resources in Finalize method rather use the DISPOSE.
What
is the purpose of the Using block in C#?
The using statement allows the programmer to specify when
objects that use resources should release them. The object provided to the
using statement must implement the IDisposable interface. This interface
provides the Dispose method, which should release the object's resources.
public class Debuggersspace : IDisposable
{
//implementation details...
}
These
are equivalent:
Debuggersspace qa = new Debuggersspace ();
try
{
qa.Action();
}
finally
{
if (qa != null)
qa.Dispose();
}
In other words, the
using (Debuggersspace qa = new Debuggersspace ())
{
qa.Action();
}
Debuggersspace qa = new Debuggersspace ();
try
{
qa.Action();
}
finally
{
if (qa != null)
qa.Dispose();
}
In other words, the
using
statement tells .NET to release the object specified in the using
block once it is no longer neededusing (Debuggersspace qa = new Debuggersspace ())
{
qa.Action();
}
Points to remember:
- If you declare the variable outside the using block and then create a new instance in the using statement it may not dispose the item
- So the using statement will automatically dispose of the object once that context is complete.
- The using statement
is used to work with an object in C# that implements the
IDisposable
interface. - The
IDisposable
interface has one public method calledDispose
that is used to dispose of the object. - we use the using statement, we don't need to explicitly dispose of the object in the code, the using statement takes care of it.
C# Disposable pattern, Using, Dispose Vs Finalize:
Please refer below links for more information:
What are the different types of variables in C#?
Different types of variables used in C# are :
static variables
instance variable
value parameters
reference parameters
array elements
output parameters
local variables
What is literals and their types?
Literals are value constants assigned to variables in a program. C# supports several types of literals are
Integer literals
Real literals
Boolean literals
Single character literals
String literals
Backslash character literals
What are the different types of Caching?
There are three types of Caching:
Output Caching: stores the responses from an asp.net page.
Fragment Caching: Only caches/stores the portion of page (User Control)
Data Caching: is Programmatic way to Cache objects for performance.
What are the different types of statements supported in C#?
Block statements
Declaration statements
Expression statements
Selection statements
Iteration statements
Jump statements
Try catch statements
Checked and unchecked
Lock statement
Subscribe to:
Posts (Atom)
How to improve applications performance which is hosted in cloud ?
Improving the performance of an application hosted in Microsoft Azure involves a combination of optimizing your application code, leveraging...
-
Connection pool is the collection of new, open and all type of database connections. Opening database connection is a time consuming op...
-
Before start the programming you need to know exactly what is the Fibonacci series, why it comes around the world?? Golden mean: ...