Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# constructor in interface

I know that you can't have a constructor in an interface, but here is what I want to do:

 interface ISomething 
 {
       void FillWithDataRow(DataRow)
 }


 class FooClass<T> where T : ISomething , new()
 {
      void BarMethod(DataRow row)
      {
           T t = new T()
           t.FillWithDataRow(row);
      }
  }

I would really like to replace ISomething's FillWithDataRow method with a constructor somehow.

That way, my member class could implement the interface and still be readonly (it can't with the FillWithDataRow method).

Does anyone have a pattern that will do what I want?

like image 728
Toto Avatar asked Nov 06 '09 08:11

Toto


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.

What is C full form?

Full form of C is “COMPILE”. One thing which was missing in C language was further added to C++ that is 'the concept of CLASSES'.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

use an abstract class instead?

you can also have your abstract class implement an interface if you want...

interface IFillable<T> {
    void FillWith(T);
}

abstract class FooClass : IFillable<DataRow> {
    public void FooClass(DataRow row){
        FillWith(row);
    }

    protected void FillWith(DataRow row);
}
like image 88
NDM Avatar answered Nov 15 '22 14:11

NDM


(I should have checked first, but I'm tired - this is mostly a duplicate.)

Either have a factory interface, or pass a Func<DataRow, T> into your constructor. (They're mostly equivalent, really. The interface is probably better for Dependency Injection whereas the delegate is less fussy.)

For example:

interface ISomething 
{      
    // Normal stuff - I assume you still need the interface
}

class Something : ISomething
{
    internal Something(DataRow row)
    {
       // ...
    }         
}

class FooClass<T> where T : ISomething , new()
{
    private readonly Func<DataRow, T> factory;

    internal FooClass(Func<DataRow, T> factory)
    {
        this.factory = factory;
    }

     void BarMethod(DataRow row)
     {
          T t = factory(row);
     }
 }

 ...

 FooClass<Something> x = new FooClass<Something>(row => new Something(row));
like image 35
Jon Skeet Avatar answered Nov 15 '22 13:11

Jon Skeet