tags:

views:

1230

answers:

17

Is there a way to separate a C# class into a header that contains the class definition and then an actual .cs file that contains the implementation? I suppose one can do this by creating an interface, but that doesn't seem right. I just want a file where I can just see the class design, and not all the detail. It's easy enough to do in C++, but I haven't seen it done with C#.

Thanks in advance.

A: 

Isn't this what the IDE is for?

EDIT: Otherwise inferfaces and abstract classes is the way to go.

Mark S. Rasmussen
+2  A: 

No there is no way (or real reason) to want to do this in C#. You can get VS.NET to summarise a class for you (collapse all in the view menu) or if you really want to as you say you can use an interface. What is the reason behind you asking?

Kieran Benton
A: 

Try the Class View. When you click on each class you will get the members listed.

Paul Batum
+9  A: 

In the same file, you can use Visual Studio outline function to collapse the class so that you only see the names of methods and properties.

You can also use Visual Studio to see the Class View which gives you the names of various methods and properties of a class.

There is almost no reason in dotnet to need to define a class in a separate place from its implementation.

Vaibhav
A: 

Why would you want to do that? You could create an abstract class and put that into a header file and subclass it in the implementation file, but there is no goo reason to do that.

Armin Ronacher
+13  A: 

You could use partial classes and partial methods. I'm not sure why you'd do this though...

Partial Classes and Methods - MSDN

muloh
A: 

The IDE will show you exactly that inline when you have an instance followed by the "." like

myBadlyNamedObject.

(or "ClassName."), and the beauty is that you have it at your fingertips when working with the object and not when you decide to open up the object definition to see what it might be

Oskar
+18  A: 

That's a wrong approach. C# isn't C++. Forget about header files.

If you want the class summary, just open the Object Browser in Visual Studio. It will give you the signature of all the methods within your classes.

Joannes Vermorel
+1  A: 

I don't think you can do header files like you can in C++. Check out the partial keyword for both classes and methods. I just learned about them yesterday, so I haven't really used them, but they might help you accomplish what you're trying to do.

Joel
A: 

As far as I know that's not possible. You can however make things a little bit better by using partial classes where you put different parts of a class in different files. You can for example put all public methods in one file and all private in one to make it easier to get an overview of which methods are available for use from other objects.

Jonas
+1  A: 

Use an interface to achieve the same intention.

IFoo.cs

public interface IFoo
{
  int DoFoo();
}

Foo.cs

pubic class Foo : IFoo
{
  public int DoFoo()
  {
    return 1;
  }
}
craigb
I'm not the one who voted you down, but I like to avoid interfaces that are only implemented by one class.
Matt Blaine
A: 

If you really, really need to do this, then the closest option would be Refactor --> Extract interface.

chris
A: 

To all you people saying "USE THE IDE~!~~", you're missing the point. Code isn't always read in the IDE. Maybe he wants it to be printed out? Or emailed? That's the problem with not implementing language features because you can get the IDE to do it: Code isn't always read (or even written) in an IDE.

That said, you can use partial classes and methods to do it; I don't think you'll be able to have instance variables in both files, however.

TraumaPony
+1  A: 

Extracting the interfaces isn't a great plan if you're interested in the private methods. Using abstract classes means materially altering the design of the application (and I think, increasing complexity needlessly) to support the "view" requirement. Partial classes don't show you the complete public and private signature in one place, so that's not ideal either.

So if you don't have the IDE, or don't want to use it, I would use the default disassemble action in Reflector (free, and a great toy to have anyway):

http://www.red-gate.com/products/reflector/index.htm

eg. System.Web.Caching.Cache

public sealed class Cache : IEnumerable
{
    // Fields
    private CacheInternal _cacheInternal;
    public static readonly DateTime NoAbsoluteExpiration;
    public static readonly TimeSpan NoSlidingExpiration;

    // Methods
    static Cache();
    [SecurityPermission(SecurityAction.Demand, Unrestricted=true)]
    public Cache();
    internal Cache(int dummy);
    public object Add(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback);
    public object Get(string key);
    internal object Get(string key, CacheGetOptions getOptions);
    public IDictionaryEnumerator GetEnumerator();
    public void Insert(string key, object value);
    public void Insert(string key, object value, CacheDependency dependencies);
    public void Insert(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration);
    public void Insert(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback);
    public object Remove(string key);
    internal void SetCacheInternal(CacheInternal cacheInternal);
    IEnumerator IEnumerable.GetEnumerator();

    // Properties
    public int Count { get; }
    public long EffectivePercentagePhysicalMemoryLimit { get; }
    public long EffectivePrivateBytesLimit { get; }
    public object this[string key] { get; set; }
}
JBE
+1  A: 

This was only a feature of C++ because ancient compilers needed a forward-declaration of the function signatures to work properly.

If this is something that you find handy to have though, and you want more than once, you could try writing a small utility that used reflection to extract the public interface from any component, and format it out to a text file in whatever layout you wanted.

Another alternative would be to use the /// syntax to create XML documentation for the class.

A: 

WinCV is a .net 1.0 framework sdk utility that gives you a c++ like header view for .net assemblies. Search google for wincv.exe on how to configure it for .net 2.0.

A: 

you can always use a partial class

tsinik