Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interface member with "this" keyword

While going through our client's code, I came across below interface in C#, which is having a member with "this" keyword.

 public interface ISettings
{
    string this[string key] { get; }
}

I am not aware of any such pattern or practice where interface member name starts with "this". To understand more, I checked the implementation of this interface, however still not able to figure out its purpose.

internal class SettingsManager : ISettings
{
    public string this[string key]
    {
        get { return ConfigurationManager.AppSettings[key]; }
    }
   ...
   ...
}

And here is the caller code:

public static class Utility
{
   public static ISettings Handler { get; set; }

    public static string Get(string key, string defaultValue)
    {
        var result = Handler[key];

        return Is.EmptyString(result) ? defaultValue : result;
    }
}

Unfortunately, I am not able to debug this code to see the things live. But very curious about it. If the implemented code is finally returning a string, then what is the use of "this" keyword out there?

like image 701
Anil Soman Avatar asked Jan 28 '14 11:01

Anil Soman


People also ask

Can this keyword be used in interface?

One thing you are missing is, that the this keyword represents the current "Object" and not current "Class". So, if and when you create an object of this "Interface" (by implementing it in another class of course), the this keyword will represent that specific object.

Which members can an interface have?

Interfaces can contain instance methods, properties, events, indexers, or any combination of those four member types. Interfaces may contain static constructors, fields, constants, or operators. Beginning with C# 11, interface members that aren't fields may be static abstract .

Is interface a keyword in C#?

In C#, an interface can be defined using the interface keyword. An interface can contain declarations of methods, properties, indexers, and events. However, it cannot contain fields, auto-implemented properties. The following interface declares some basic functionalities for the file operations.

CAN interface have variables in C#?

In C#, you are allowed to create a reference variable of an interface type or in other words, you are allowed to create an interface reference variable. Such kind of variable can refer to any object that implements its interface.


2 Answers

It enables you to do things like:

SettingsManager settings = new SettingsManager();
var setting = settings["my setting"];

A common use is with the List<T> class.

It has the definition:

public class List<T> : IList<T>, ICollection<T>, IList, ICollection, IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable
{
    // ....

    public T this[int index] { get; set; }

    // ....
}

This allows you to 'index' the internal values in a similar way to an array.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace test
{
    static class Program
    {
        static void Main()
        {
            List<string> myStrings = new List<string>();

            myStrings.Add("abc");
            myStrings.Add("def");

            Console.WriteLine(myStrings[0]); // outputs: "abc"
            Console.WriteLine(myStrings[1]); // outputs: "def"

            Console.Read();
        }
    }
}
like image 102
rhughes Avatar answered Oct 18 '22 20:10

rhughes


They are indexers, allowing to access your class like an array, in the example your provided you see the usage in this line:

var result = Handler[key];
like image 45
jnovo Avatar answered Oct 18 '22 21:10

jnovo