4

I'm using ASP.NET and I know that I can mark a method as:

But I'm asking if I can mark a method as "Not yet used", that means "I wrote it but actually it is unused, sooner or later someone will using it".

Thanks!

Community
  • 1
  • 1
Rowandish
  • 2,655
  • 3
  • 31
  • 52
  • 3
    What is the point in showing that a method is not used at all? If it is an internal or private member you get VS warning about unused member, for public members you can only use ReSharper. However as long as you do not want to delete the method that is not used where exactly is the use for such a feature? – MakePeaceGreatAgain Feb 09 '16 at 11:00
  • If you just want some kind of note or checklist you can always use the special VS TODO: comment, or any of the other comment keywords that are appropriate. – Sam Axe Feb 09 '16 at 11:41
  • I had a new property which I was implementing in some code. It would have been used in a ton of places. I marked it as "obsolete" even though it was going to be a NEW property. It was going to take some doing to set the value on it correctly in a number of places, so in the meantime I set it to an empty string. I wasn't sure the impact of this new property, and it gave me an excellent indication of the effort required to set it properly when the time comes. – LarryBud Dec 24 '21 at 12:27

6 Answers6

12

You can throw a NotImplementedException with a appropriate message in your method. This will ensure during runtime that the method is not called.

public void MyMethod(){
  throw new NotImplementedException("This will be implemented soon");
}

If you want to use metainformation you can define your own Attribute to decorate your methods. (But I think you can also reuse the ObsoleteAttribute with a special message)

Jehof
  • 34,674
  • 10
  • 123
  • 155
  • OP states he has already written that method but doesn't call it. Throwing an exception in a method you don't call is not very helpful. – Patrick Hofman Feb 09 '16 at 10:58
  • @PatrickHofman Sure. It ensures only that the method is not called during runtime (exception is thrown). If he uses the Exception in his methods he could use normal Visual Studio tools to find all references of NotImplementedException. – Jehof Feb 09 '16 at 11:02
  • The mention of a special attribute 'fixes' the problem I had with your answer. Retracted downvote. – Patrick Hofman Feb 09 '16 at 11:06
6

No, you cannot. And it doesn't make much sense to do so.

Visual Studio 2015 does show you the method usage inside your current project, which is an indicator for 'dead' methods:

enter image description here

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
2

I assume you want it for a future functionality that currently is not active because is not fully implemented (is the only case I can imagine that make sense).

If you don't want it to be used don't put it in the interface (you should use interfaces) or put it in a distinct one that only you have. You can also use the Obsolete decorator with a "this is not meant to be used yet" message.

If anyone can use it because it's public and released you should not care about how many references it has. Usually you can't know it if the references are from outside your solution. You should not delete such a method because that is a breaking change.

If it's private your IDE should warn you and you can delete it or comment its future usage; add a reference to the functionality it is meant for so you can delete it in case it is closed (someone decides that is not going to be implemented).

Dodger
  • 342
  • 3
  • 9
1

Just because you can doesn't mean you should....
Why write code that's not yet used?

That's like taking thermal underwear to the Sahara in case of a cold snap, when you're only visiting during the day.

While you can throw a NotImplementedException this won't give you compile time safety - it will just throw this exception on run time.

Alex
  • 37,502
  • 51
  • 204
  • 332
1

This is managed by the NotImplementedException and can be very usefull when implementing interfaces where you know the members you need in the future but don´t want to implement them right now. Any user of the method will get the exception, however if you need a compile-time flag you´d be off by using the Obsolete-attribute together with a meaningul message such as "This method is not yet implemented" and the exception inside.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
  • This is kinda rediculous but this question has once again proven that programmers can't read: http://haacked.com/archive/2007/02/27/Why_Cant_Programmers._Read.aspx/ – MarengoHue Feb 09 '16 at 11:20
1

You can express all of your sorrow about the unused method in the Documentation Comment for this method. Just don't throw any NotImplementedException's or else this method isn't going to be used ever.

This is kinda dumb though, you should remove it if it is not used. As practice shows, most of the 'Not yet, but maybe some day' code is not going to ever be used at all, so it makes perfect sense to remove it. If for some reason you later need the method, you can always check your source control for that.

If you would like to litter your solution even more though, you can always do something stupid like implement your custom [PleaseUseMe]Attribute

MarengoHue
  • 1,789
  • 13
  • 34