2

Windows 8 アプリ ストア コードに type-o がありました。奇妙な結果が得られたので、もう一度調べてみると、値が間違っていることに気付きましたが、それでもコンパイルしてエラーなしで実行できました。これはおかしいと思い、Windows 8 コンソール アプリケーションで試してみましたが、そのコンテキストではコンパイル エラーです。何を与える?

アプリストアのバージョン:

var image = new TextBlock()
            {
                Text = "A",    //Text is "A"
                FontSize =     //FontSize is set to 100
                Height = 100,  //Height is NaN
                Width = 100,   //Width is 100
                Foreground= new SolidColorBrush(Colors.Blue)
            };

コンソール版:

public class test
{
    public int test1 { get; set; }
    public int test2 { get; set; }
    public int test3 { get; set; }
    public int test4 { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        test testObject = new test()
                          {
                              test1 = 5,
                              test2 =
                              test3 = 6, //<-The name 'test3' does not exist in the current context                         
                              test4 = 7
                          };
    }
}
4

1 に答える 1

5

コードの最初のブロックが配置されたクラスには というプロパティがあったと推測しているHeightため、コンパイラはそれを次のように解釈していました。

var image = new TextBlock()
            {
              Text = "A",
              FontSize = this.Height = 100,
              Width = 100,
              Foreground = new SolidColorBrush(Colors.Blue)
            };

image.Heightそれはまた、あなたのプロパティがなぜだったのかを説明するでしょうNaN- あなたのイニシャライザはそれを設定しようとしませんでした。

一方、Programコードの 2 番目のブロックが配置されているクラスには、 という名前のメンバーがないtest3ため、コンパイラはそれに対してバーフィードを行いました。

古い学校のプロパティ割り当てとして初期化子コードを書き直すと、問題はより明確になります。

test testObject = new test();
testObject.test1 = 5;
testObject.test2 = test3 = 6; // What is test3?
testObject.test4 = 7;
于 2013-05-12T00:47:04.023 に答える