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

How to convert "19/01/2014" into 19-Jan-2014?

We will use ToString method of DateTime object to convert date into specific format like below:

dt.ToString("dd-MMM-yyyy")


Output would be:19-Jan-2014
How to convert string date into datetime?

DateTime'Parse method is used to convert string Date into Datetime.

For Example:-

string date = "2014-01-01";

DateTime dt = DateTime.Parse(date);

How many ways of converting string date into datetime?

NOTE: This is objective type question, Please click question title for correct answer.
How to check whether string has been assigned a value or not?

With the help of IsNullOrEmpty method of string,we can check whether string has a value or not.It returns True or False.
Basically IsNullOrEmpty checks whether string contains value or not.

For Example:-

string str = null;


if(string.IsNullOrEmpty(str))
MessageBox.Show('value is null');
else
MessageBox.Show('value is not null');


Output would be 'value is null' because str varaible has not assigned any value.
How do yo define generics in C#?

Generics are the most powerful feature in C# 2.0.
Allows us to define type-safe data structures, without committing to actual data types. this results in significant performance as we can re-use data-processing algorithms without duplicating type specific code.Generics are similar to C++ templates, but are drastically different in implementation and capabilities
What are the advantages you get by using generics?

1.Lets you to re-use the code and the effort that you put into implementing it.
2.The types and internal data can change, regardless of whether you are using value type or reference type.
3.Performance is greatly improved as the generic code does not force boxing and unboxing of value types, or downcasting of reference types.
What is the default value of string and integer in DotNet?

Default value of string is Null and integer is 0.
If we do not assign any value at the time of declaration of string and integer variables then by-default string variables be assigned as Null and integer variables be assigned as 0 at run-time.
What is the difference between scope_identity() and current_identity()?

Both of them are similar and they will return the last identity value generated in the table.
The difference is that scope_identity() returns the identity value that is currently in scope.
How to replace characters in a string?

With the help of Replace method we can repace any word from withing string value. It takes 2 parameters i.e. old_value and new_value.

For Example:-

string st_name = "Rajesh Sathua";

st_name = st_name.Replace("Rajesh", "Rajesh Kumar");


Output would be:Rajesh Kumar Sathua
What is the difference between String and StringBuilder?

Strings are immutable means every-time memory is allocated in the Heap.Each time string creates new string in the memory.
But StringBuilder is mutable object.It does not create new string in the memory rather than update the data in the same memory in the heap.

In short,in case of String every-time new string values are added in the memory and discarded the old value.
But in case of StringBuilder,every-time string is added into existing string.

For Example: String:-
string str = "Rajesh Kumar";

str = str + " Sathua";

//In above code,Rajesh Kumar will be discarded in the memory and new value Rajesh Kumar Sathua will be allocated to Heap.

StringBuilder:-

StringBuilder sb = new StringBuilder();
sb.Append("Rajesh Kumar");
sb.Append(" Sathua");


//In above code,Rajesh Kumar will be allocated in the memory and new value Sathua will be appended to Rajesh Kumar and Rajesh Kumar Sathua will be added in Existing Memory.
How to add string values in StringBuilder.

There is a StringBuilder'Append method ,which is used to add string value in StringBuilder object.

StringBuilder sb = new StringBuilder();

sb.Append("Rajesh Kumar");
sb.Append(" Sathua");
MessageBox.Show(sb.ToString());


//Output would be:Rajesh Kumar Sathua
What is the use of AppendLine in StringBuilder?

AppendLine method is used to Append string value in a New Line.

StringBuilder sb = new StringBuilder();

sb.AppendLine("Rajesh Kumar");
sb.AppendLine(" Sathua");
MessageBox.Show(sb.ToString());


//Output would be:Rajesh Kumar
Sathua //new line
What is the use of AppendFormat method in StringBuilder?

AppendFormat is used for Formatting purpose meaning we can format our string as we want.


For Example:-

StringBuilder sb = new StringBuilder();

sb.AppendFormat("First Name:{0},Last Name:{1}", "Rajesh", "Sathua");
Response.Write(sb1.ToString());


Output would be:-
First Name:Rajesh,Last Name:Sathua

What is an alternate way of checking whether string is Empty or not?

We can check whether string is Empty or not with the help of String'Length property .
For Example:-

string name = "Rajesh Kumar Sathua";

if (name.Length > 0)
{
MessageBox.Show("string has a value");
}
else
{
MessageBox.Show("string is empty");
}

How to format string in Dot Net?

With the help of String.Format method,we can format any string in Dot Net like below:-

string str = string.Format("My Name is:{0} and Age is:{1}", "Rajesh", "29");

Response.Write(str);


Output:My Name is:Rajesh and Age is:29
How to format below Date in yyyy-MM-dd format with the help of String.Format method? DateTime dt_todays_date = DateTime.Now;

We will use below code to format Date as

string str = string.Format("{0:yyyy-MM-dd}", dt_todays_date);

Response.Write(str);


Output:2013-01-21
What will happen when we run below code? string str = null; Response.Write(str.ToString());

It will throw an error which says that 'Object reference not set to an instance of an object'
Because if the object is null or not assigned any value to it and we are trying to convert it with .ToString then it will throw an error.Because .ToString does not handle Null value.Thats why it will throw an error.
So to avoid such exception,we have to use Convert.ToString() method as it handles Null value.
When does below message occur? Object reference not set to an instance of an object

When we create any Class object but do not initialize with New keyword and we are dealing with that object or we are using that object anywhere in our code,then above error occurs.

For Example:-
DataTable dt_project = null;

dt_project.Columns.Add("project_id");
dt_project.Columns.Add("project_name");


In above code,we have not initialized datatable object and still adding columns to it.When we run,it will throw above error.
How to allow only numeric values in Textbox?

We will use RegularExpressionValidator control to achieve.

<asp:TextBox runat="server" id="txt_version" />

<asp:RegularExpressionValidator runat="server" id="rex_version" ControlToValidate="txt_version" ValidationExpression="^[0-9]$" ErrorMessage="Only Numeric values allowed" />

How to allow only 5 digit numeric value in Textbox using Regular Expression Validator control?

We will write below code in RegularExpressionValidator control to achieve.

<asp:TextBox runat="server" id="txt_version" />

<asp:RegularExpressionValidator runat="server" id="rex_version" ControlToValidate="txt_version" ValidationExpression="^[0-9]{5}$" ErrorMessage="Only Numeric values allowed" />

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