class Program
{
class B
{
[DefaultValue(2)]
public int x, y;
[DefaultValue(5)]
public int z;
}
static void Main(string[] args)
{
B b = new B();
Console.WriteLine(String.Format("{0} {1} {2}", b.x, b.y, b.z));
}
}
결과 : 0, 0, 0DefaultValue는 콘솔 응용 프로그램에서 작동합니까?
이 DefaultValueAttribute
콘솔 응용 프로그램에서 작동하지 않는, 또는 내가 뭔가 잘못을했다?
편집 :
누군가 get,set
와 특성에 대해 말했지만,이 역시 작동하지 않습니다
class Program
{
public class A
{
[DefaultValue("123")]
public string Name { get; set; }
}
static void Main(string[] args)
{
var a = new A();
Console.WriteLine(a.Name);
}
}
결과 : 빈 문자열
내 질문이 'Name {get; 설정;}'. 그것은 재산인가? 그렇지 않다면, 예를 보여주세요, 이해할 수 없네요. – FSou1
@ FSou1 : 네, 그건 재산입니다. Andrey의 답변에서 읽을 수 있듯이, 'DefaultValue'는 실제로 아무 것도하지 않습니다. – ProgramFOX