누구나 설명 할 수 있습니까? 이유는 "개인 읽기 전용 Int32 [] _array = new [] {8, 7, 5};" null 일 수 있을지 어떨지초기화 된 읽기 전용 필드가 null 인 이유는 무엇입니까?
이 예에서는 작동하며 _array는 항상 null이 아닙니다. 하지만 내 회사 코드에서 나는 simliar 코드가 있고 _array는 항상 null입니다. 그래서 저는 그것을 정적이라고 선언해야했습니다.
클래스는 내 WCF 계약의 부분 프록시 클래스입니다.
using System;
using System.ComponentModel;
namespace NullProblem
{
internal class Program
{
private static void Main(string[] args)
{
var myClass = new MyClass();
// Null Exception in coperate code
int first = myClass.First;
// Works
int firstStatic = myClass.FirstStatic;
}
}
// My partial implemantation
public partial class MyClass
{
private readonly Int32[] _array = new[] {8, 7, 5};
private static readonly Int32[] _arrayStatic = new[] {8, 7, 5};
public int First
{
get { return _array[0]; }
}
public int FirstStatic
{
get { return _arrayStatic[0]; }
}
}
// from WebService Reference.cs
public partial class MyClass : INotifyPropertyChanged
{
// a lot of Stuff
#region Implementation of INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
}
그것은 우리에게 작동하는 코드를 보여 작동하지 않는 다른 코드가 거기에 말을 많이 사용하지 않습니다. 당신은 우리에게 짧지 만 완전한 예제를 제공 할 필요가 있습니다. –
코드가 아니라 여기에서 작동하는 경우 코드에 문제가 있음을 분명히 알 수 있습니다. 게시하지 않으면 어떤 도움도 제공하기가 어렵습니다. – Gorpik
LINQPad에서 의도 한대로 작동합니다. – Davio