1. Is string reference type / value type?
Ans: It's a reference type.
More about string:
The string type
represents a sequence of zero or more Unicode characters. string is
an alias for String in
the .NET Framework.
The
equality operators (== and !=) are defined to compare
the values of string objects, not references.
The String object is immutable. Every time you use one of the methods in the System.String class, you create a new string object in memory, which requires a new allocation of space for that new object. For example, when
you write this code, the compiler actually creates a new string object to hold
the new sequence of characters, and that new object is assigned to b. The
string "h" is then eligible for garbage collection.
string b = "h";
b += "ello";
2. Difference between .ToString() and Convert.ToString().
Ans: The basic
difference between .ToString() and Convert.ToString()
is
Convert.ToString() handle null values and .ToString() throw a null reference
exception.
//Returns
a null reference exception for str.
string str;
object i = null;
str = i.ToString();
//Returns
an empty string for str and does not throw an exception. If you dont
string str;
object i = null;
str = Convert.ToString(i);
1.
|
Which of
the following statements are true about the C#.NET code given below?
String str1,
str2;
str1 =
"Hi";
str2 =
"Hi";
|
Ans: 2. Only one object will get created.
3.The string
built using the String class are immutable (unchangeable), whereas, the ones
built- using the StringBuilder class are mutable.
Ans: True.
Ans: E. A String is created on the heap. A String has a zero-based index.
5.What is difference between value and reference types?
Value Type: Variables that are based on value types directly contain values. Assigning one value type variable to another copies the contained value.
int a = 5;
a==b and a.Equals(b) returns
true , because in this case both compare two object by value.
The String object
is immutable. Every time you use one of the methods in the System.String class,
you create a new string object in memory, which requires a new allocation of
space for that new object. In situations where you need to perform repeated
modifications to a string, the overhead associated with creating a new String object
can be costly. TheSystem.Text.StringBuilder class
can be used when you want to modify a string without creating a new object. For
example, using the StringBuilder class
can boost performance when concatenating many strings together in a loop.
4. Which
of the following statements about a String is correct?
|
||||||||||
|
5.What is difference between value and reference types?
Value Type: Variables that are based on value types directly contain values. Assigning one value type variable to another copies the contained value.
- Value type are stored on stack
- Value types derive from System.ValueType
- Value type consists of primitive data types, structures, enumerations.
- Value types
can not contain the value null.However,
the nullable types feature
does allow for value types to be assigned to null.
Reference Type: Reference
types store the address of their data on the stack. The actual data that the
address refers to is stored in an area of memory called the heap. Because
reference types represent the address of the data rather than the data itself,
assigning a reference variable to another doesn’t copy the data.
- Reference type are stored on heap.
- Reference types derive from System.Object
- Reference type consists of class, array, interface, delegates.
- Reference types can contain the value null.
object obj1 = "String1";
//
create a new string; assign obj1 the reference to that new string
"String1"
object obj2 = obj1;
// copy
the reference from obj1 and assign into obj2; obj2 now refers to
// the same string instance
6. What
is difference between a.equals(b) and a==b?
In .Net Framework data types can be
classified according to whether a variable of a particular type stores its own
data or a pointer to the data. If the variable stores its own data, it is a
value type and if it holds a pointer to data elsewhere in memory it is a
reference type. You can assign either a reference type or a value type to a
variable of the Object data type
The Equals() and == operator are
used for comparison and both returns the boolean value (true/false).
Value Type
For Value Type == and Equals()
works in the same way, both compare two object by value.
int b = 5;
Reference
Type
In case of Reference Type both
works in different way.
StringBuilder sb1 = new StringBuilder("Asp.Net");
StringBuilder sb2 = new StringBuilder("Asp.Net");
sb1 == sb2 returns false and
sb1.Equals(sb2) returns true.
== operator compares reference
returns true when both references point to the same object and Equals()
compares object by value and it will return true if the references refers
object which are equivalent.
No comments:
Post a Comment