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

Can you prevent your class from being inherited by another class?

Yes. The keyword “sealed” will prevent the class from being inherited.
Can you allow a class to be inherited, but prevent the method from being over-ridden?

Yes. Just leave the class public and make the method sealed.
What are the different ways a method can be overloaded?

Different parameter data types, different number of parameters, different order of parameters.
What is the use of CommandBehavior.CloseConnection?

We can use pass it with ExecuteReader method of Command object like

reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

This will make sure that when we are calling reader.Close() the associated connection object will also be closed.
How to declare a property in a class?

int m_PersonID = 0;

public int PersonID
{
get { return m_PersonID; }
set { m_PersonID = value; }
}
How to declare a property in an Interface?

DateTime DateOfBirth { get;set;}
int Age { get;set;}
string FirstName { get;set;}


As this is an Interface, so no implementation required only definition of properties are required. Implementation of these properties will be written into the class inherting this interface.
How to declare methods into an Interface

void Calculate();
int Insert(string firstName, string lastName, int age);

Interface doesn't contain implementation so methods implementation will be written into class inherting this interface.
How to load assembly from GAC?

Lets say you have to load the assembly from GAC on button click event then you should write following method.

protected void btn_Click(object sender, EventArgs e)


{

AssemblyName asm = new AssemblyName("ClassLibrary1, Version=1.1.0.0, Culture=neutral,
PublicKeyToken=fbc28d9ca2fc8db5");

Assembly al = Assembly.Load(asm);



Type t = al.GetType("ClassLibrary1.Class1");

MethodInfo m = t.GetMethod("Method1");

str = "reflection - " + (string)m.Invoke(null, null);

MessageBox.Show(str);

}


For more visit http://www.infosysblogs.com/microsoft/2007/04/loading_multiple_versions_of_s.html
Difference Between dispose and finalize method?

Dispose is a method for realse from the memory for an object.

For eg:

<object>.Dispose.

Finalize is used to fire when the object is going to realize the memory.We can set a alert message to says that this object is going to dispose.
What does the term immutable mean?

The data value may not be changed. Note: The variable value may be changed, but the original immutable data value was discarded and a new data value was created in memory.
What’s the difference between System.String and System.Text.StringBuilder classes?

System.String is immutable. System.StringBuilder was designed with the purpose of having a mutable string where a variety of operations can be performed.
Are private class-level variables inherited?

Yes, but they are not accessible. Although they are not visible or accessible via the class interface, they are inherited.
What happens if you inherit multiple interfaces and they have conflicting method names?

It’s up to you to implement the method inside your own class, so implementation is left entirely up to you. This might cause a problem on a higher-level scale if similarly named methods from different interfaces expect different data, but as far as compiler cares you’re okay.
To Do: Investigate
Types of Errors ?

Configuration Errors
Parser Errors
Compilation Errors
Run-Time Errors
Difference between classes and structures?

A struct is a value type and a class is a reference type.

When we instantiate a class, memory will be allocated on the heap and when struct gets initiated it gets memory on the stack.

Classes can have explicit parameter less constructors. But structs dosn't have this.

Classes support inheritance. No inheritance for structs.

A struct cannot inherit from another struct or class, and it cannot be the base of a class. Like classes, structures can implement interfaces.
How to convert a sentence into Title Case (Capitalize first character of every word)?

Use ToTitleCase method.
System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(&quot;dotnetfunda.com is a very good website&quot;);


The output will be "Dotnetfunda.Com Is A Very Good Website"
How to sort an array into descending order?

First sort the array using Array.Sort and then use Array.Reverse

Eg.


int[] number = new int[] {6, 4, 7};
Array.Sort(number); // sort in ascending order
foreach (int i in number)
{
lblMessage.Text += i + " <br />";
}
// reverse ie descending order
Array.Reverse(number);
lblMessage.Text += "<hr />";
foreach (int i in number)
{
lblMessage.Text += i + " <br />";
}

How to read entire stream contents?

If you have a stream object (any of the stream like StreamReader, MemoryStream, FileStream) and you want to show its content, write following code.


// here theStream is the stream object

while (myStream.Position != myStream.Length)
{
Console.WriteLine("{0:x2}", myStream.ReadByte());
}

Write a single line of code to read the entire content of the text file.

User following code

string strContent = System.IO.File.ReadAllText(Server.MapPath("~/MyTextFile.txt"));
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