Basic C# Interview Questions on strings


What is the difference between string keyword and System.String class?
string keyword is an alias for Syste.String class. Therefore, System.String and string keyword are the same, and you can use whichever naming convention you prefer. The String class provides many methods for safely creating, manipulating, and comparing strings.

Are string objects mutable or immutable?
String objects are immutable.

What do you mean by String objects are immutable?
String objects are immutable means, they cannot be changed after they have been created. All of the String methods and C# operators that appear to modify a string actually return the results in a new string object. In the following example, when the contents of s1 and s2 are concatenated to form a single string, the two original strings are unmodified. The += operator creates a new string that contains the combined contents. That new object is assigned to the variable s1, and the original object that was assigned to s1 is released for garbage collection because no other variable holds a reference to it.

string s1 = "First String ";
string s2 = "Second String";

// Concatenate s1 and s2. This actually creates a new
// string object and stores it in s1, releasing the
// reference to the original object.
s1 += s2;

System.Console.WriteLine(s1);
// Output: First String Second String


What will be the output of the following code?
string str1 = "Hello ";
string str2 = s1;
str1 = str1 + "C#";
System.Console.WriteLine(s2);

The output of the above code is "Hello" and not "Hello C#". This is bcos, if you create a reference to a string, and then "modify" the original string, the reference will continue to point to the original object instead of the new object that was created when the string was modified.

What is a verbatim string literal and why do we use it?
The "@" symbol is the verbatim string literal. Use verbatim strings for convenience and better readability when the string text contains backslash characters, for example in file paths. Because verbatim strings preserve new line characters as part of the string text, they can be used to initialize multiline strings. Use double quotation marks to embed a quotation mark inside a verbatim string. The following example shows some common uses for verbatim strings:

string ImagePath = @"C:\Images\Buttons\SaveButton.jpg";
//Output: C:\Images\Buttons\SaveButton.jpg

string MultiLineText = @"This is multiline
Text written to be in
three lines.";
/* Output:
This is multiline
Text written to be in
three lines.
*/

string DoubleQuotesString = @"My Name is ""Venkat.""";
//Output: My Name is "Venkat."

13 comments:

  1. for this code (take from above):
    string str1 = "Hello ";
    string str2 = s1; //should this be str1 not s1 ?
    str1 = str1 + "C#";
    System.Console.WriteLine(s2);
    // Should the line above be str2 and not s2 ?
    Assuming s should be str.. are u saying that str2 is just a reference to str1? Does this also mean the contents or actual string were not copied to str2? assuming all the above, what if garbage collection takes place then str2 be null becuase the first str1 was created again, and its original value was discarded?

    ReplyDelete
    Replies
    1. I know this answer is too late for the ichi, but for the benefit of others I am replying. Assigning str1 to str2, the value is not copied instead now both str1 and str2 reference the same object in memory. If garbage collection takes place, it would not collect anything because though str1 no more refer to the old object, but str2 still refers the old object. As long as the reference count for an object in memory is not reduced to 0, gc wont collect it as it is still referenced by someone who may need it.

      Delete
    2. Now your this reply making it a bit more confusing... we know string is immutable thats why it will create a new copy instead appending the str2 on the same object as str1 has, and don't forget why we use StringBuilder coz it appends in the same memory.
      So the question is= below example;
      str1="hello";
      str2=str1+"hello2";
      str3=str2+"hello3"';
      str4=str3+"hello4";

      GC will never be called as main object in the memory "hello" will always be pointing somewhere to run the working of str4??
      so what does actually the immutable and mutable mean is??




      Delete
  2. string is value type or reference type??? from the above example??

    ReplyDelete
    Replies
    1. Yonatan Arbel - IsraelDecember 26, 2014 at 2:37 AM

      string is always a reference type as it's array of char.

      Delete
  3. string is reference type

    ReplyDelete
    Replies
    1. string is a reference type because string is a class and class is always a reference type.

      Delete
  4. What will be the output of the following code?
    string str1 = "Hello ";
    string str2 = s1;
    str1 = str1 + "C#";
    System.Console.WriteLine(s2);


    I think you may have a typo string str2 = s1; should be string str2 = str1;

    ReplyDelete
  5. the difference between String class and string instance is
    Class provides some static safe methods for immutable modifications
    and Instance provides the instance method..

    Can anyone tell me..
    1. how to deal with Equal Method
    2. In which scenario to use string builder and in which string..

    ReplyDelete
  6. String object is a immutable whare StringBuffer object is mutable.because in String object we cannot modify the content,where in StringBuffer we can modify the content.

    ReplyDelete
  7. can any one give example for StringBuffer object

    Thanks&Regards
    Hareesh.A

    ReplyDelete
  8. HI,
    Thanks for such wonderful videos, it is very help full for lerning.
    i have a small request if you could upload a project that utilizes all the topics discussed here, it would be very helpful.
    Thanks and regards.

    ReplyDelete
  9. Its not StringBuffer , it is StringBuilder

    ReplyDelete

If you are aware of any other C# questions asked in an interview, please post them below. If you find anything missing or wrong, please feel free to correct by submitting the form below.

 
Disclaimer - Terms of use - Contact Us