C# Interview Questions and Answers (958) - Page 10

What is CharEnumerator in C#?

CharEnumerator is an object in C# that can be used to enumerate through all the characters of a string. Below is the sample to do that


string str = "my name";

CharEnumerator chs = str.GetEnumerator();
while(chs.MoveNext())
{
Response.Write(chs.Current);
}


Moderator: please do not move this to Code section, this is a typical interview question that was asked :)
Why 'static' keyword is used before Main()?

Program execution starts from Main(). S it should be called before the creation of any object. By using static the Main() can now be called directly through the class name and there is no need to create the object to call it. So Main() is declared as static.
Which methods are used to execute javascript code from code behind file?

RegisterStartupScript and RegisterClientScriptBlock
What is the main difference between RegisterStartUpScript and RegisterClientScriptBlock?

RegisterStartUpScript Add the javascript code at the end of the page before the closing of form tag. while RegisterClientScriptBlock place the code at th top of the page.
What is the page size in SQL Server?

8 KB -- > 8192
you can check this with the query :
SELECT low FROM master..spt_values WHERE number = 1 AND type = 'E'
Can we use Page.PreviousPage to get the value of controls of the previous page in case of response.Redirect ?

No, we can't use PreviousPage to find the control on previous page with response.redirect.
what is the class used for dealing with graphics objects

System.Drawing.Graphics is the class used for dealing with graphics objects
Write a way to read an image from a path to a bitmap object

We can directly read an image file and load it into a Bitmap object as below.
Bitmap myBmpObj = Bitmap.FromFile(strPath);


We can also set Image path during the Initilization of Bitmap Object. Code as given below.
Bitmap loBMP = new Bitmap(strPath);





Modified By:
Moderator3
Name any two "Permission Classes" generally used while working with CAS ?

1) FileIOPermission
2) UIPermission
3) SecurityPermission
Does Abstract classes contain Constructor?

NOTE: This is objective type question, Please click question title for correct answer.
Can we inherit a private class into the public class?

No, In C#, a derived class can’t be more accessible than it’s base class. It means that you can't inherit a private class into a public class. So always the base class should be more or equal accessible than a derived class.

// Following will throw compilation error
private class BaseClass

{
}
public class DerivedClass : BaseClass
{
}


// Following will compile without an error
public class BaseClass

{
}
public class DerivedClass : BaseClass
{
}


Hope this helps
Can we declare a class as Protected?

No, a class can not be declared as Protected.

A class can be declared with only following access modifiers.

Abstract
Internal
Public
Sealed
Static
Extern (The extern modifier is used to declare a method that is implemented externally. - http://msdn.microsoft.com/en-us/library/e59b22c5(VS.80).aspx)

Thanks
What is the difference between IQueryable<T> and IEnumerable<T> interface?

IEnumerable<T>
IEnumerable<T> is best suitable for working with in-memory collection.
IEnumerable<T> doesn't move between items, it is forward only collection.

IQueryable<T>
IQueryable<T> best suits for remote data source, like a database or web service.
IQueryable<T> is a very powerful feature that enables a variety of interesting deferred execution scenarios (like paging and composition based queries).

So when you have to simply iterate through the in-memory collection, use IEnumerable<T>, if you need to do any manipulation with the collection like Dataset and other data sources, use IQueryable<T>.

Please let me know your feedback, if any
Some facts about Virtual methods.

Virtual keyword is used to declare a method inside the base class that can be overridden.

Following conditions are applied to a virtual method

1. Virtual method can not be declared as Private
2. Virtual method can not be declared as Static
3. Virtual method may or may not be overridden.
4. In order to override the virtual method of the base class, Override keyword is used provided the method signature of the derived class is same as base class, even the access modifier should be same.
5. A Virtual method must contain the method body otherwise compiler shall throw error.
6. Virtual keyword is only used in base and derived class scenario where you want your method over-ridable in the derived class.

Enjoy virtual keyword :)
Explain About ref and out parameters?

ref parameter:
Ref is keyword.
it must be used along with actual and formal arguments.
ref variables must be initialized before passing to a function

out parameter:
It is very similar to call by Reference.
Out is a Keyword.
Which must be used along with actual and formal arguments.
Out variables are not require to initialize, the initialized value will not be passed, only reference will be passed.
Why C# is called Strongly Typed Language?

C# is called Strongly Typed Language becausen its type rules are very strict. For example you can't called a function that is designed to call Integer with a string or decimal. If you want to do so then you will have to explicitly convert them to integer.
Example for Compile time Polymorphism and Example for Run time Polymorphism?

Example of Compile Time Polymorphism - Method Overloading
Example of Run Time Polymorphism - Method Overriding
What is Closed constructed type and Open constructed type?

Closed constructed type and open constructed type is term that is used while instantiated an object.

For exmaple
class myClassO<T>

{

}
Class MyClassC<int>
{

}


In above code class myClassO is the open constructed type as we can pass any type of parameter while instantiating the myClassO object.
myClassO<int> i = new myClassO<int>(20); // Correct

myClassO<string> i = new myClassO<strubg>('my string"); // Correct

You can notice that I can pass either integer or string in the myClassO insntiation as this is the generic class.

myClassC is the closed constructed type as I have specified the type of argument to be passed in the myClassC instantiation.

myClassC<int> i = new myClassC<int>(20); // Correct

myClassC<string> i = new myClassC<strubg>('my string"); // InCorrect

Found this useful, bookmark this page to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape

 Interview Questions and Answers Categories