Я изучаю (новичок).NET, и у меня есть некоторые сомнения.
Чтение из примеров книг Я узнал, что String - это объект, а затем Тип ссылки.
Итак, я сделал этот тест, и результат был другим, что я ожидал:
Мне действительно интересно, это исключение, потому что "строка" - это специальные типы?
class Program
{
static void Main(string[] args)
{
SByte a = 0;
Byte b = 0;
Int16 c = 0;
Int32 d = 0;
Int64 e = 0;
string s = "";
Exception ex = new Exception();
object[] types = { a, b, c, d, e, s, ex };
// C#
foreach (object o in types)
{
string type;
if (o.GetType().IsValueType)
type = "Value type";
else
type = "Reference Type";
Console.WriteLine("{0}: {1}", o.GetType(), type);
}
// Test if change
string str = "I'll never will change!";
Program.changeMe(str);
Console.WriteLine(str);
}
public static string changeMe(string param)
{
param = "I have changed you!!!";
return ""; // no return for test
}
}
Вывод:
System.SByte: Value type
System.Byte: Value type
System.Int16: Value type
System.Int32: Value type
System.Int64: Value type
System.String: Reference Type
System.Exception: Reference Type
I'll never will change!