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

What is the use of Dispose() method of streamreader class?

Dispose as name says,disposes all resources used by the System.IO.Streamreader object.When the streamreader object is disposed then it also closes the underlying stream.
Actually Dispose() is implemented in TextReader class,which is the base class for StreamReader.

For Example:
string file_contents;

using (StreamReader sr = new StreamReader("C:\\rpt.txt"))
{
file_contents = sr.ReadToEnd();
sr.Dispose();
}

What is the use of StreamWriter in C#?

StreamWriter is a class used in Dot Net to write Text into files.As name suggests it writes data to a byte stream.Actually it implements a System.IO.TextWriter class that writes characters to a byte stream in a particular encoding.

We have to give a proper file path in its constructor otherwise it will give FileNotFound error.

Syntax:
DirectoryInfo[] dirs = new DirectoryInfo(@"c:\").GetDirectories();


using (StreamWriter sw = new StreamWriter("name_of_the_file"))
{
foreach (DirectoryInfo dir in dirs)
{
sw.WriteLine(dir.Name);
}
}

Differentiate IS and AS operators?

The IS operator checks whether an object is compatible with a given type, and the result of evaluation will be boolean: true/false and also the IS operator will never throw an exception.
The AS operator is like a cast except that it yields NULL on conversion failure instead of returning an exception.
What do we mean by read-only property?

Read-Only property is also known as Get property.
The get property is enclosed in curly braces({ }) and it must return value.It can access any member on the class.

Syntax:-
get{return variable_name}
What do we mean by Properties in Dot Net?

Properties are a special kind of class members.Dot Net provides Set and Get methods to access and modify any value.Property reads and writes get and set method.Thats why Get method is also called as Read-Only property and Set is also known as Write-Only property.

Usually inside a class,we declare a private variable and will provide a set of public SET and GET methods to access the variable.

For Example:

private string _first_name; 


public string First_Name
{
get {return _first_name;}
set {_first_name = value;}
}

What do we mean by Set property in Dot net?

Set property is also very useful in Dot Net.It is also known as Write-Only property.We use this property to set or assign any value to any variable from anywhere in the class.
The set property is also enclosed in curly braces({ }) and it must be assigned any value.

Syntax:-
set { variable_name = value; }


Where value is we assign value to variable_name.
Give a simple example of Get and Set property?


public class Project_Master
{
private string _project_name;
public string Project_Name
{
get
{
return _project_name;
}
set
{
_project_name = value;
}
}
}


For assigning value to Project_Name property as
Project_Name = "abc"; - Set Property

to get thr value from Project_Name
string prj_name = Project_Name; - Get Property
How to write only Readonly property in C#?

Simply write get property as
private string _last_name = "Kumar";  

public string Last_Name
{
get
{
return _last_name;
}
}

List All Class Access Specifiers?

There are five access specifiers in Visual Basic .NET defined as follows:

Public – Applied at the class level and is the most common specifier. Public Classes are classes that are intended to be used by any consumer.
Public Class PubClass
End Class

Protected – can only be applied to Nested Classes. Are only accessible within the class and within the child classes.
Public Class HasProtected
Protected Class ProtectedClass
End Class
End Class

Friend – Are accessible only within the program in which they are defined. If you add the Friend access specifier to a class definition, instance of that class can only be created from within th same program.
Friend Class FriendClass
Public Shared Sub PublicMethod()
MsgBox(“FriendClass.PublicMethod”)
End Sub
End Class

Protected Friend – Represents a union of the Protected and Friend Specifiers. Protected class must be nested class, thus Protected Friend class must be nested too.
Public Class HasProtectedFriend
Protected Friend Class ProtectedFriend
Public Shared Sub Test()
MsgBox(“test”)
End Sub
End Class
End Class

Private – Can only be applied to a nested class. A Private nested class represents implementation details of the class. When you have complex problem that requires more problem solving horsepower then simple methods can provided, defining a nested private class that implements the abstraction.
Public Class HasPrivate
Private Class PrivateClass
End Class
End Class
what are Overridable, MustOverride and NotOverridable modifiers?

Overridable – modifier indicates that a method can be overriden.
NotOverridable - modifier indicates that you can not override a method.
MustOverride - modifier indicates that a method is abstract, and child class must implement the MustOverride method in a parent class.
what Shadows modifier

Shadows – if you want a child class to use a name previously used in a parent class, use the Shadows keyword to do so. Shadows keyword simply allows you to reintroduce a previously used name in the child class without a compiler error.
Difference between a Class and an object

Class:
1)It is a datatype that contains the programming logic.
2)Class is visible in the source code and resides in hard disk.
3)Class is like a template or blueprint of the object. It implements reusability,
encapsulation, inheritance

example:Button is a class with properties like Text,BackColor,
events like click, methods like Focus

Object:

1)it is a chunk of memory that implements the class logic.
2)Object is in the RAM and not visible in source code.
3)It is the real world implementation of the class.
Each object has its own copy of data.
example: Button1, Button2 are the objects of Button class.
What are the different ways a method can be overloaded

Different parameter data types, different number of parameters, different order of
parameters.

example:
int area(int a, int b) 

{
return a*b; --different number of parameters
}
int area(int b)
{
return a*a;
}
parameter return types

int calc(int a)
{
return a;
}

double calc(double b)
{
return b*5;
}

Diversities between an abstract method & virtual method ?

An Abstract method does not provide an implementation and forces overriding to the deriving class (unless the deriving class also an abstract class), where as the virtual method has an implementation and leaves an option to override it in the deriving class. Thus Virtual method has an implementation & provides the derived class with the option of overriding it.

Abstract method does not provide an implementation & forces the derived class to override the method.
What do we mean by Anonymous Delegate?

Anonymous methods provide a technique to pass a code block as a delegate parameter.Anonymous methods are basically methods without a name,we can say it has just the body.
There is no return type in an anonymous method.It is inferred from the return statement inside the method body.

In other words,we can say that an anonymous method or delegate is an inline unnamed method in the code.It is created using the delegate keyword and doe not required name and return type.Hence we

can say,an anonymous method has only body without name,optional parameters and return type.
An anonymous method behaves like a regular method and allows us to write inline code in place of explicitly named methods.
What are the features of anonymous delegate or method?

Following are the features of anonymous delegate or method:

1. We can access any variable inside the anonymous method which are declared outside of the anonymous method.
2. We use anonymous method when working with event handling
3. An anonymous method when declared without parenthesis can be assigned to a delegate with any signature.
4. Unsafe code can’t be accessed within an anonymous method.
5. An anonymous method can’t access the ref or out parameters of an outer scope.
Attributes can be applied to ?

NOTE: This is objective type question, Please click question title for correct answer.
Can multiple catch blocks that correspond to a single try block executed ? try { ///some error code } catch(Exception1 ex) { ///some code } catch(Exception2 ex) { ///some code } catch(Exception3 ex) { ///some code } catch(Exception4 ex) { ///some code }

No, It is not possible. Whenever there is an error, it will immediately look for the catch block assuming it is there. It cannot go back to the try block and check for the second exception.
Global.asax file can be downloaded via the Url like : www.samplewebsite.com/Global.asax True or False

NOTE: This is objective type question, Please click question title for correct answer.
How can we get the length of the string in Dot Net?

We have string'Length property which is used to return the Length of a given string.It's an in-built property of string object.
For Example:
string str = "Vishal";

int length = str.Length;

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