簡體   English   中英

C# 代碼使用 OpenTK

[英]C# code using OpenTK

我剛開始學習 C# 和 OpenTK(我已經知道 Java 和 C++)。 我在 OpenTK 提供的演示代碼中遇到了這行代碼:

if (Keyboard[Key.Escape])
            Exit();

如果按下 Esc 按鈕,Keyboard[Key.Escape] 將返回 true。 但是,我不認識這種語法。 鍵盤不是數組。 誰能向我解釋一下這種語法是什么以及它是如何工作的? 參考鏈接就足夠了。 感謝您的時間。

在 c# 中,任何 object 都可以實現啟用括號 [] 語法的索引屬性,這就是這里發生的事情。 以下是一個簡單的示例——雖然它顯然不是傳統意義上的數組,但它仍然具有可用的索引器語法。 在您的情況下,該屬性看起來是 boolean:

class Foo
{
    private string _foo; 

    public Foo(string foo)
    {
        _foo = foo; 
    }

    public bool this[string foo]  // the indexer can be anything
    {
        get                  // the getter can work however the programmer wants
        {
            return _foo == foo;
        }
    }
}

可以這樣使用:

        Foo f = new Foo("Hello World!");

        bool foo = f["Hello World!"]; // will return true

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM