using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class User
{
public int? Age { get; set; }
public int? ID { get; set; }
}
class Program
{
static void Main(string[] args)
{
User user = new User();
user.Age = null; // no warning or error
user.ID = (int?)null; // no warning or error
string result = string.Empty;
User user2 = new User
{
Age = string.IsNullOrEmpty(result) ? null : Int32.Parse(result),
// Error 1 Type of conditional expression cannot be determined
// because there is no implicit conversion between '<null>' and 'int'
// ConsoleApplication1\ConsoleApplication1\Program.cs 23 71 ConsoleApplication1
ID = string.IsNullOrEmpty(result) ? (int?)null : Int32.Parse(result) // // no warning or error
};
}
}
}
질문 :왜 개체 이니셜 라이저를 사용할 때 use (int?) null입니까?
다음 줄이 작동하지 않는 이유는 무엇입니까?
Age = string.IsNullOrEmpty(result) ? null : Int32.Parse(result)
// 수정 한
Age = string.IsNullOrEmpty(result) ? (int?) null : Int32.Parse(result)
이유는 다음 줄의 작품이다?
user.Age = null; // no warning or error
참조 http://stackoverflow.com/questions/1171717 –