Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does System.Convert fit OO conventions?

Tags:

c#

oop

Aren't classes supposed to be called after objects and not actions? It just does not sit along with OO theory I learned.

One thought was that maybe since Convert holds only static methods then no instances of it are supposed to be created at all, which might make it an acceptable exception.

like image 826
Quaker Avatar asked Mar 04 '14 09:03

Quaker


2 Answers

In C# you can't create methods outside classes or structs. So when you want to create utility methods, it's best to store them in a static class.

So i would say that there's nothing object oriented about a static class.

like image 110
Raz Megrelidze Avatar answered Oct 07 '22 18:10

Raz Megrelidze


There's nothing object-oriented about the Convert class, because object-orientation is about data with behaviour, and the Convert class holds no data or state.

It's basically a practical consideration.

In Eiffel, for example, the conversion methods would have been defined in a base class, and all classes needing to use the conversion methods would have derived from that base class. However, Eiffel has multiple inheritance, so that makes sense for Eiffel, but not for a language like C# where you don't have multiple inheritance.

like image 44
Mark Seemann Avatar answered Oct 07 '22 17:10

Mark Seemann