"this" in C#

1.Indexers
Indexers allow instances of a class or struct to be indexed just like arrays.

2.Extension Methods
Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.The most common extension methods are the LINQ standard query operators that add query functionality to the existing System.Collections.IEnumerable and System.Collections.Generic.IEnumerable<T> types.

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/indexers/
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/params

-------------------------
1.Indexers
using System;
class SampleCollection<T>
{
   // Declare an array to store the data elements.
   private T[] arr = new T[100];
   // Define the indexer to allow client code to use [] notation.
//The this keyword is used to define the indexer.
//The value keyword is used to define the value being assigned by the set indexer.  
  public T this[int i]
   {
      get { return arr[i]; }
      set { arr[i] = value; }
   }
}
class Program
{
   static void Main()
   {
      var stringCollection = new SampleCollection<string>();
      stringCollection[0] = "Hello, World";
      Console.WriteLine(stringCollection[0]);
   }
}
-------------------------
2.Extension Methods
namespace Jin.Test
{
    public static class DoubleExtension
    {
        public static double Round(this double input, int digit)
        {
            double outI;
            outI = System.Math.Round(input, digit);
            return outI;
        }
    }
}

double d = System.Math.PI;
double d1 = d.Round(4);

------------------
Reference:
https://www.ecma-international.org/publications/files/ECMA-ST/Ecma-334.pdf
 https://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf

@Deprecated and [Obsolete]
[Obsolete] //in C#, @Deprecated in java 
public double GetArea() 
The following warning message will be reported at compile time:
The GetArea() method is obsolete.

评论

此博客中的热门博文

XML, XSL, HTML

Input in element.eleme.io

Data URI是由RFC 2397 ACE