I am following the Preamble: what is a reference type? where it explains parameters passing as value or reference type. The first example has the following code:
using System;
using System.Text;
public class Example1
{
public static void Main (string[] args)
{
StringBuilder first = new StringBuilder();
StringBuilder second = first;
first.Append ("hello");
first = null;
Console.WriteLine (second);
}
}
And it does not provide the source code for the class (or struct) of StringBuilder, and I do not know how the Console.WriteLine(second) is able to return a string value just by using the identifier. Is it possible to return a value in the constructor?
I tried to write the class or struct by following (and it doesn’t work):
struct StringBuilder
{
private string _myString;
public string StringBuilder
{
get { return _myString; }
set { _myString = value; }
}
public void Append(string str)
{
_myString = str;
}
}
Have a look a the documentation for
Console.WriteLine(object value). It says this:So (as others have written), if you want to print a string representation of your own class or struct, you should override the ToString() method.