2

Can anyone explain the below code can any one give some real time explanation with below code snippets

public interface roman
{
    roman this[string name] { get; }
}

public class notempty : roman
{
    public string Color{ get; set; }

    public roman this[string name]
    {
        get { return new notempty() { Color= "Value1" }; }
    }
}
P P P
  • 227
  • 3
  • 16
Maddy
  • 48
  • 5
  • It would help if you explained which bits you do and dont understand – Justin Nov 17 '11 at 13:22
  • 1
    real time explanation? Do you want to teleconference? – Stealth Rabbi Nov 17 '11 at 13:23
  • I'd recommend you to read this.. http://stackoverflow.com/questions/2697783/what-does-program-to-interfaces-not-implementations-mean/2697810#2697810 – this. __curious_geek Nov 17 '11 at 13:23
  • I can tell you that most of the stuff in that snippet does not comply with naming conventions. Class names, constructors, methods and public properties must use PascalCasing. Aside from that, you have a class that derives from an interface, that's pretty basic stuff. – Matteo Mosca Nov 17 '11 at 13:25
  • Don't try to understand it, it is a pretty broken way to implement a class factory. Google "c# class factory pattern" to find the proper way. – Hans Passant Nov 17 '11 at 13:29
  • 2
    @MatteoMosca must is a pretty strong word. – user247702 Nov 17 '11 at 13:29
  • @Stijn I know. And I meant to use it. C# has its naming conventions, like every other language. Those conventions make code easier to read and understand. There are even tools, like resharper, that can spot all naming conventions errors and fix them for you, if you're too lazy to do that. – Matteo Mosca Nov 17 '11 at 13:37

2 Answers2

3
public interface roman // there is an interface called "roman", accessible to all
{
    // all implementations of "roman" must have an "indexer" that takes a string
    // and returns another "roman" instance (it is not required to offer a "set")
    // typical usage:
    //     roman obj = ...
    //     roman anotherObj = obj["abc"];
    roman this[string name] { get; }
}

public class notempty : roman // there is a class "notempty", accessible to all,
{                             // which implements the "roman" interface

    // no constructors are declared, so there is a default public parameterless
    // constructor which simply calls the parameterless base-constructor

    // any instance of "notempty" has a string property called "Color" which can
    // be both read (get) and written (set) by any callers; there
    // is also a *field* for this, but the compiler is handling that for us
    public string Color{ get; set; }

    // there is an indexer that takes a string and returns a "roman"; since
    // there is no *explicit* implementation, this will also be used to satisfy
    // the "roman" indexer, aka "implicit interface implementation"
    public roman this[string name]
    {
        // when the indexer is invoked, the string parameter is disregarded; a
        // new "notempty" instance is created via the parameterless constructor,
        // and the "Color" property is assigned the string "Value1"; this is then
        // returned as "roman", which it is known to implement
        get { return new notempty() { Color= "Value1" }; }
    }
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
2

The Inferface roman defines that all implementations should have an indexer this[string name] that returns an instance of roman.

Check out:

C# Station Tutorial - Interfaces

and

C# Station Tutorial - Indexers

hope this helps

dknaack
  • 60,192
  • 27
  • 155
  • 202