Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between implementing an interface and applying an attribute in C#

Tags:

This might be a stupid question but I'll ask anyway,

I was reading "OOP Demystified: A Self-Teaching Guide by Jim Keogh and Mario Giannini" chapter 11 which covers interfaces. The examples in this book are C++.

I noticed that C++ uses ISerializable to make a class serializable which you would implement where as in C# you just attribute the class with the [Serializable] attribute.

What is the key difference here? Is it that with an interface you must provide the implementation where as if you attribute something the compiler will work out the implementation for you?

I guess that with the [Serializable] attribute the .Net framework uses reflection to make the serialized object from the actual object.

That said is it possible in that case to have an [Disposable] attribute or using my theory above the framework wont know how to actually dispose of an object hence you have to do it yourself?

Would be grateful for a clarification.

like image 641
Remotec Avatar asked Apr 19 '10 13:04

Remotec


People also ask

What does it mean to implement an interface?

An Interface is a specification of functionality that a class MUST implement. When you implement an interface, you are specifying to any consumers of your class that you supply the functionality defined in the given Interface.

Is it necessary to implement all methods of an interface in C#?

Yes, it is mandatory to implement all the methods in a class that implements an interface until and unless that class is declared as an abstract class.

Can an interface have attributes C#?

C# Language Attributes Reading an attribute from interface There is no simple way to obtain attributes from an interface, since classes does not inherit attributes from an interface. Whenever implementing an interface or overriding members in a derived class, you need to re-declare the attributes.

What is the purpose of interface in C#?

An interface may not declare instance data such as fields, auto-implemented properties, or property-like events. By using interfaces, you can, for example, include behavior from multiple sources in a class. That capability is important in C# because the language doesn't support multiple inheritance of classes.


1 Answers

A long time ago in a galaxy far, far away... There were no Attributes or compiler support for class metadata, so the developers tried to implement their own. One of the methods our ancestors worked out were to declare Marker Interfaces .

So, to answer your question: custom attributes are an "evolution" of marker interfaces. You can use both. But note that, if you want to enforce that your object implement specific methods, you are using an interface, plain and simple. That's how IDisposable works, it forces you to implement a method named Dispose(). [Serializable] (and probably ISerializable on your C++ example) does not force you to implement anything, as the runtime will just read that declaration and do its task (i.e., serialize the object).

Note that C# also have a ISerializable interface... It is meant to let you write your custom serialization code, which will then be called by the runtime. Note that it is NOT a marker interface nor replacement for the [Serializable] attribute, as you still need to mark your class with the attribute for the serialization to work.

like image 141
Fábio Batista Avatar answered Oct 23 '22 22:10

Fábio Batista