От: | desco | http://v2matveev.blogspot.com | |
Дата: | 11.01.07 21:52 | ||
Оценка: | 7 (1) |
using System;
class Program
{
private static string GetTypeName<T>(T obj)
{
return typeof(T).FullName;
}
static void Main(string[] args)
{
Console.WriteLine(GetTypeName((object)null + null)); (1)
Console.WriteLine(((object)null + null).GetType());
Console.WriteLine(GetTypeName(null + (object)null)); (2)
Console.WriteLine((null + (object)null).GetType());
}
}
С какого такого перепугу компилятор вывел тип выражения (1) как object, тогда как в (2) — string. По идее в данном случае должны быть использованы перегрузки оператора + для строк (выделенные варианты) и соответственно типы обоих выражений — stringSystem.Object
System.String
System.String
System.String
Press any key to continue . . .
string operator +(string x, string y);
string operator +(string x, object y);
string operator +(object x, string y);
The binary + operator performs string concatenation when one or both operands are of type string. If
an operand of string concatenation is null, an empty string is substituted. Otherwise, any non-string
operand is converted to its string representation by invoking the virtual ToString method inherited
from type object. If ToString returns null, an empty string is substituted.
<skipped>
The result of the string concatenation operator is a string that consists of the characters of the left
operand followed by the characters of the right operand.