0

I was learning about NUnit and found we use class decorators like [TestMethod] before every test function.

Then i learnt about decorators at this link. According to this answer

When You add decorator in C# it is like adding a property to the class/method. There will be an attribute attached to it.

I just want to ask if I want to use property to be used in whole class (not talking about one method) then why don't I choose inheritance over class decorator

What is the difference ...

tbhaxor
  • 1,659
  • 2
  • 13
  • 43
  • 1
    They are called *Attributes*. They are metadata and can be applied to a class, method, property etc. Inheritance and attributes are like oranges and apples; cannot be compared. – CodingYoshi Oct 13 '18 at 02:50
  • Possible duplicate of [Decorators versus inheritance](https://stackoverflow.com/questions/6406446/decorators-versus-inheritance) – Kevin Avignon Oct 13 '18 at 02:55

2 Answers2

3

Be careful because the link you provided is using the word Decorator to refer to 2 entirely different concepts. The question in that link is asking this question:

What does it mean to Decorate a class or parameter?

What's the purpose and when would I do this?

Some answers, answer the question correctly specifically this and this. Both of these are talking about the Decorator Pattern.

However, this answer is talking about something entirely different. Although decorator can be used to mean Attributes (the ones with square brackets in C#) but that SO thread is using it to mean both the Decorator Pattern and Attributes.


Attributes

Attributes are explained here.


Decorator Pattern

Decorator Pattern pattern is explained here.


Decorator Pattern vs Inheritance

There is a good comparison here.

Community
  • 1
  • 1
CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
1

The main difference is there could be exponentially many sub classes if you use inheritance over decorator.

The main point of using attribute decorator like that is for cross-cutting concern, you can reuse the same decorator across many classes & methods (reusable)

In your case, it's entirely different. [TestMethod] is used as a "marker" instead of as a decorator to let the framework know which method is a test method. I don't see any relationship to inheritance in this case.

You could also use interfaces as "marker", but it's not as flexible as using attributes in this case.

Khanh TO
  • 48,509
  • 13
  • 99
  • 115
  • Can you please explain ? I didn't get what are you saying – tbhaxor Oct 13 '18 at 02:52
  • @TeraByte: Let's say if you have 3 decorators & 5 classes that you can apply to, you have to write only 8 classes. But if you use inheritance, you need to have 3x5 (15 classes) and the number could be exponential if you have more classes and functionality. – Khanh TO Oct 13 '18 at 03:05
  • @TeraByte: but in your example of `[TestMethod]`, it's more like a "marker" attribute instead of class decorator. I don't see any relationship to inheritance in that case – Khanh TO Oct 13 '18 at 03:11