views:

396

answers:

12

Is there a concept in C# of class definition and implementation similar to what you find in C++?

I prefer to keep my class definitions simple by removing most, if no every, implementations details (it depends on several factors as you may know, but generally I move towards leaving most member implementation details outside the class definition). This has the benefit of giving me a bird's eye view of the class and its functionality.

However in C# it seems I'm forced to define my member functions at the point of declaration. Can this be avoided, or circumvent some way?

During my apprenticeship of C#, this is one aspect that is bothering me. Classes, especially complex ones, become increasingly harder to read.

+3  A: 

Define an interface.

Then it's nice to be able to automatically implement the interface using a nice code assist tool.

SevenCentral
Or an abstract class. But that would be dumb.
Philippe
+3  A: 

You could get a similar result by defining an interface for each of your classes which they then implement.

JDunkerley
Ok. I think I can see your point. Use as my types interfaces to classes, instead of classes. Not exactly what I was hoping for, but sure does the trick. It also answered my question. :) There isn't a way to separate member implementation details from the class definition.
Krugar
Interfaces really aren't the correct approach here as they can only contain the **public** members of the class. There is no way to achieve the same result as a header file in C#.
Scott Dorman
I wouldn't go after this principle with my eyes closed. You should not define an interface for the class just to make it easier for you to read. defining interfaces all over the place can make the maintenance of your code a nightmare.
Moshe Levi
Agreed definitely. Why I registered the possibility but don't actually intend to implement it :)
Krugar
+3  A: 

It sounds like you're referring to interfaces. In c#, you can define all of your member functions in an interface, and then implement them in another class.

Robert Harvey
You can define all of your **public** member functions in an interface. You can not, however, define any accessibility.
Scott Dorman
+6  A: 

If you're using Visual Studio, you can take advantage of the Class View. You can also use the expand/collapse features of the source code editor.

In the improbable case that your tools don't help, you can always write a quick utility that will summarize the class for you.

If the class has been compiled, you can use Reflector to view the class, too.

John Fisher
Good point John! The class viewer indeed.
Krugar
Also, the Class Diagram tool is pretty good for getting a birdseye view.
Tim Long
A: 

The prototyping that I guess you are referring to does not really exist in C#. Defining interfaces as others have suggested will give you a point where you have declarations of your methods collected, but it's not the same thing as prototypes, and I am not so sure that it will help you in making your implementation classes easier to read.

C# is not C++, and should probably not be treated as C++.

Fredrik Mörk
The interface will only contain the **public** methods of the class.
Scott Dorman
@Scott: yes, that is true, that strengthens the argument that it does not quite solve the issue raised in the question.
Fredrik Mörk
+4  A: 

No, there is no concept of implementation and header files in C# like you find in C/C++. The closest you can come to this is to use an interface, but the interface can only define the public members of your class. You would then end up with a 1-to-1 mapping of classes and interfaces, which really isn't the intent for how interfaces are to be used.

Scott Dorman
+1  A: 

If you find that a class is hard to read or difficult to understand, that's often a sign that the class is trying to do too much. Instead of trying to duplicate C++'s separation of declarations and definitions, consider refactoring the troublesome class into several classes so that each class has less responsibility.

bcat
It's very debatable bcat. It depends on the problem domain.
Krugar
But it is worth considering and keeping in mind.
Snarfblam
+1  A: 

Whenever it's possible or desirable, I'll go with the previous responses and define an interface. but it's not always appropriate.

alternatively, you can work around this "problem" by using some static code inspection tools. Resharper's "File Structure" window will give you exactly what you want. you can also use the built in "Class View" from visual studio. but I prefer the former.

Moshe Levi
+3  A: 

In C# you could fake it with partial classes and partial members to a point, however, forward declarations and prototypes go the way of the dodo bird with your newer languages. Class View, Class Diagrams, Intellisense, et al, all help to remove the potential need for those "features".

sixlettervariables
A: 

There are two remedies for this to make it more C++-ish:

  • Create an interface file that declares all method signatures and properties
  • Implement that interface in a class across multiple files by using the partial modifier on the class definitions

Edits:

// File: ICppLikeInterface.cs
public interface ICppLikeInterface
{
    ...
}

// File: CppLikeImplementation1.cs    
public partial class CppLikeImplementation : ICppLikeInterface
{
    ...
}

// File: CppLikeImplementation2.cs    
public partial class CppLikeImplementation : ICppLikeInterface
{
    ...
}

The C++ way of separating interface into a header file is mostly (I think) due to an early design decision when C was created to allow fast, incremental compilations during the "old days", as the compiler throws away any meta data, contrary to Smalltalk. This is not a matter with C# (nor Java) where tens of thousands of lines compiles within seconds on recent hardware (C++ still doesn't)

Cecil Has a Name
A: 

Not sure what you mean by your classes continue to grow and become hard to read. Do you mean you want a header file like view of a class's members? If so, like John suggested, can't you just collapse the implementation so you don't have to see it?

If you don't want every class to implement a certain thing, then interfaces are probably the way to go (like others are saying).

But as a side thought, if your classes themselves get more and more complex as a your write the program, perhaps it's more of a design issue than a language problem? I think a class should have one responsibility and not take on more and more responsibilities as the program grows, rather the number of classes and how old classes are used should grow and get more complex as you continue to develop your software?

Evan
You seem to be wanting to implement the notion of Contract to a class definition. Such refinement is not always possible, or easy to achieve. A Bird class needs to fly, court, poo, breed, walk, eat.
Krugar
I don't mean a 'contract' notion, just that 'tested' (or already implemented code) should be left alone (if possible) so that a program is easier to read and maintain.For example, if you want to add functionality to Bird, why not just give Bird a collection of Behavoirs (an interface). Each behavior (fly/poop/etc) would implement Behavoirs and would be implemented outside of the Bird, so Bird's implementation wouldn't have to change and would still get new functionality.I agree it's not always possible, but if you can predict how your program will change over time, you can design for that?
Evan
+3  A: 

This is really a case of needing to step back and see the bigger picture. Visual studio has many, many tools to help you write and manipulate your code, from outlining, #regions, class view, class diagrams, the Code Definition Window and many more.

C# isn't C++, if you try to make it so then you'll trip over yourself and no-one else will be able to read your code.

A day spent learning to use the Visual Studio tools will repay the investment many times over in terms of productivity and you'll soon wonder how you ever lived with that C++ way of doing things.

Tim Long
I'm not entirely disagreeing with you Tim. But while that is all fine and dandy, what this philosophy essentially means is that only in the presence of a feature rich IDE can I make head and tails of my code. Now... that doesn't give me much comfort.
Krugar