Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Idiomatic way of signaling unimplemented methods in C#

Tags:

idioms

c#

I'm building the skeleton for a C# app and intend to leave a bunch of methods without implementation - returning dummy values. I intend to get back to them, but don't want to accidentally forget to implement any of them.

I'd like to signal when I reach a method that isn't implemented, and continue execution with the dummy value.

What's the idiomatic way of doing this?

like image 419
Luchian Grigore Avatar asked Aug 15 '12 07:08

Luchian Grigore


2 Answers

The classic way to do this would be:

throw new NotImplementedException();

which is clear to the caller and easy to find later to fix (in fact, it shows up automatically on some task lists). However, if that isn't an option, maybe:

return 0; // TODO

again, this will show up automatically on tasks lists, and is easily found.

If you want something more obvious:

[Obsolete("not implemented")]
public int Foo() {
    return 0;
}

which will appear as a compiler warning at the caller, or:

public int Foo() {
    #warning Foo not implemented
    return 0;
}

which will appear as a compiler warning at the method.

like image 75
Marc Gravell Avatar answered Sep 18 '22 15:09

Marc Gravell


Traditionally you'd throw a NotImplementedException for a stub but this, of course, will terminate execution of that path. Other options would include:

Just logging it. Stick a line into your log or console output.

Or, depending on how much pop-ups annoy you, you could use Debug.Assert/Debug.Fail to pop a message when these methods are hit.

like image 21
Alexander R Avatar answered Sep 21 '22 15:09

Alexander R