2016-07-20 11 views
4

をどういう意味:この[文字列キー]私は<code>Microsoft.AspNetCore.Http</code>アセンブリから<code>IRequestCookieCollection</code>コードを見

// 
    // Summary: 
    //  Represents the HttpRequest cookie collection 
    [DefaultMember("Item")] 
    public interface IRequestCookieCollection : IEnumerable<KeyValuePair<string, string>>, IEnumerable 
    { 
    // 
    // Summary: 
    //  Gets the value with the specified key. 
    // 
    // Parameters: 
    // key: 
    //  The key of the value to get. 
    // 
    // Returns: 
    //  The element with the specified key, or string.Empty if the key is not present. 
    // 
    // Exceptions: 
    // T:System.ArgumentNullException: 
    //  key is null. 
    // 
    // Remarks: 
    //  Microsoft.AspNetCore.Http.IRequestCookieCollection has a different indexer contract 
    //  than System.Collections.Generic.IDictionary`2, as it will return string.Empty 
    //  for missing entries rather than throwing an Exception. 
    string this[string key] { get; } 

    // 
    // Summary: 
    //  Gets the number of elements contained in the Microsoft.AspNetCore.Http.IRequestCookieCollection. 
    // 
    // Returns: 
    //  The number of elements contained in the Microsoft.AspNetCore.Http.IRequestCookieCollection. 
    int Count { get; } 
    // 
    // Summary: 
    //  Gets an System.Collections.Generic.ICollection`1 containing the keys of the Microsoft.AspNetCore.Http.IRequestCookieCollection. 
    // 
    // Returns: 
    //  An System.Collections.Generic.ICollection`1 containing the keys of the object 
    //  that implements Microsoft.AspNetCore.Http.IRequestCookieCollection. 
    ICollection<string> Keys { get; } 

    // 
    // Summary: 
    //  Determines whether the Microsoft.AspNetCore.Http.IRequestCookieCollection contains 
    //  an element with the specified key. 
    // 
    // Parameters: 
    // key: 
    //  The key to locate in the Microsoft.AspNetCore.Http.IRequestCookieCollection. 
    // 
    // Returns: 
    //  true if the Microsoft.AspNetCore.Http.IRequestCookieCollection contains an element 
    //  with the key; otherwise, false. 
    // 
    // Exceptions: 
    // T:System.ArgumentNullException: 
    //  key is null. 
    bool ContainsKey(string key); 
    // 
    // Summary: 
    //  Gets the value associated with the specified key. 
    // 
    // Parameters: 
    // key: 
    //  The key of the value to get. 
    // 
    // value: 
    //  The key of the value to get. When this method returns, the value associated with 
    //  the specified key, if the key is found; otherwise, the default value for the 
    //  type of the value parameter. This parameter is passed uninitialized. 
    // 
    // Returns: 
    //  true if the object that implements Microsoft.AspNetCore.Http.IRequestCookieCollection 
    //  contains an element with the specified key; otherwise, false. 
    // 
    // Exceptions: 
    // T:System.ArgumentNullException: 
    //  key is null. 
    bool TryGetValue(string key, out string value); 
    } 

とどのような声明

this[string key] 

手段を理解することができませんでした。誰かが私に説明してください。

答えて

9

それはindexerです。それは、例えばDictionary<string,T>用ようobjectName["key"]を使用して、オブジェクトのコレクションにアクセスするために使用することができるインデックス付きプロパティを定義します。それは、実装するオブジェクトは、文字列の上にインデックスを付けコレクションであることを意味します

string this[string key] 
{ 
    get 
    { 
     switch(key) 
     { 
      case "Length": 
       return this.Length; 
      case "Timeout": 
       return this.Timeout.ToString(); 
      case "Version": 
       return "1.5.0"; 
     } 
     return null; 
    } 
} 
0

string this[string key] 
{ 
    get{return _internalDictionary[key];} 
} 

またはこの:

実装は、次のようなものを見ることができます。例えば辞書のように。

1

これは、物体がアレイ状にインデックスを付けることを可能にするインデクサです。

public class MyIndexer 
    { 
     private string[] myData; 
     public string this[int ind] 
     { 
      get 
      { 
       return myData[ind]; 
      } 
      set 
      { 
       myData[ind] = value; 
      } 
     } 
    } 

public class UseIndex 
    { 
     public void UseIndexer() 
     {    
      MyIndexer ind = new MyIndexer(); 

      ind[1] = "Value 1"; 
      ind[2] = "Value 2"; 
      ind[3] = "Value 3";  
      ind[4] = "Value 4";  
      ind[5] = "Value 5";  
     } 
    } 
1

それはちょうど方法のようだが、異なる

これは本当に機能だけの特別な種類です。

class MyClass { 
    public string GetValue(string name) { 
     switch(key) 
     { 
      case "Name": 
       return "John"; 
      case "Age": 
       return 30; 
     } 
    } 
} 

あなたは、もちろんこれは次のようになり、このコードを呼び出すような方法:あなたが「インデクサー」を使用しているように

// Calling a regular method 
var instance = new MyClass(); 
var value = instance.GetValue("Name"); 
Console.WriteLine(value); 
// Output: John 

今すぐ物事のカップルを変更するたとえば、あなたがこのクラスを持っていたと想像代わりに構文。あなたの代わりにメソッドのインデクサーを使用する必要がある場合は

// Calling a regular method 
var instance = new MyClass(); 

// Remove the dot (.) and the function name 
// Instead of parenthesis use square brackets 

// OLD: var value = instance.GetValue("Name"); 
var value = instance["Name"]; 

Console.WriteLine(value); 
// Output: John 

class MyClass { 
    // Instead of using the method name, use the "this" keyword. 
    // Instead of parenthesis use square brackets 

    // OLD: public string GetValue(string name) { 

    public string this[string name] { 
     switch(key) 
     { 
      case "Name": 
       return "John"; 
      case "Age": 
       return 30; 
     } 
    } 
} 

今、あなたは、このようなコードを呼び出すでしょうか?

したい時はいつでも。あなたが感じるたびにそれは理にかなっています。これは通常、辞書<、>のようなときにオブジェクトを格納動的な値を使用しています、またはあなたはそれがリスト<>のような配列のように動作したい場合などです。

関連する問題