namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
User user =null;
user.ToHelloWorld ();
if (user == null)
{
Console.Write("world");
Console.Read();
}
else
{
Console.Write(user.UserName);
Console.Read();
}
}
catch
{
Console.Write("hello world");
Console.Read();
}
}
}
public class User
{
public string UserId { get; set; }
public string UserName { get; set; }
}
public static class UserExtention
{
public static void ToHelloWorld(this User user)
{
if (user == null)
{
user = new User() { UserName = "hello" };
}
}
}
}
这段代码的输出结果为WORLD。调试发现在执行完 user.ToHelloWorld ();后User还是为NULL。请问大神们这是为什么?
一只斗牛犬
MYYA
郎朗坤
慕工程0101907
月关宝盒
随时随地看视频慕课网APP
相关分类