开发者

C# code using OpenTK

I just started learning C# and OpenTK (I already know Java and C++). I came across this line of code in the demo code provided by OpenTK:

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

Keyboard开发者_高级运维[Key.Escape] will return true if Esc button is being pressed. I don't recognize this syntax, however. Keyboard is not an array. Can anyone explain to me what this syntax is called and how does it work? Link to reference would be sufficient. Thank you for your time.


In c# any object can implement an indexed property enabling the bracket [] syntax, and thats whats going on here. The following is a simple example -- while its obviously not an array in the traditional sense it still has the indexer syntax available. In your case it looks like the property is a 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;
        }
    }
}

Which can be used like so:

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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜