C # constructor using dynamic vs interface as parameter

In order to create clean, decoupled code in C #, I was hoping to get some feedback on using a dynamic parameter to build objects. As a rule, I believe that you will create an interface and will use the interface as a contract, but then you need to create interfaces for all your classes, which, I think, are ridiculous ...

So my question is what are the pros and cons of doing something like this:

class Class1
{
    public string Description { get; set; }
    public string Name { get; set; }

    public Class1(dynamic obj)
    {
        Name = obj.Name;
        Description = obj.Description;
    }
}

against

class Class1
{
    public string Description { get; set; }
    public string Name { get; set; }

    public Class1(IClass1 obj)
    {
        Name = obj.Name;
        Description = obj.Description;
    }
}
+5
source share
2 answers

Advantages of the interface:

  • The compiler will tell you if you are using the wrong argument
  • The signature of the constructor tells you what is required of the parameter

dynamic:

  • Name Description
  • , Name Description

# , - (, COM DLR)... , . :)

+7

, , , , .

, , , , , , , , .

, , 5 , , .

, , . .

+1

All Articles