Informational observations

There are some messages that the C# compiler could give that really wouldn't fall into the warning or error camp.  These would fall into the category of more informational, like "hey did you know that..." and would refer to issues that you could choose to address or not with no ill affects.  A warning would be something like:

public class Class {

    private int property;

    public int Property {

        get {

            return Property;




"Hey! Returning the containing property could potentially lead to an infinite recursion at runtime".


public class Class {

    int value;

    public Class(int value) {

        value = value;



"Hey! Assigning a value into itself has no affect."

These are warnings on perfectly legal code but in most cases it's probably not what you wanted to do and usually indicates an error.  So while the message might be spam, it seems a reasonable tradeoff to give it to you.

An informational message is more like:

using System.Collections;
public class Class {}

"The 'using System.Collections;' state was unncessary and can be removed".

I see this sort of message as different than before because it's not really telling you of a potential problem in your code.  (Unless you expected that System.Collections was going to be used and now you're shocked that it wasn't).   It's also not really a risky thing to tell the user.  If you remove the "using" and then attempt to use a type from that namespace (like IList) we'll then give you the smart tag immediately to add it in, so it's not going to be really annoying to be adding/removing namespaces all the time.

Would these kind of messages be appreciated?  If so, would there be others that you'd find useful?  Tiny little notes that aren't necessary but would help you out with commong coding scenarios, etc.

Comments (28)
  1. Tim P. says:

    I was thinking of the same thing today. It would be nice to have Visual Studio underline all the unused ‘using’ directives similar to the underlining of unused variables. This leads to me to a question, too. Does adding unneeded ‘using’ directives hurt performance at all?

  2. Tim: That’s somewhat of a loaded question. The short answer is "yes". The medium answer is:

    yes, it definitely hurts performance. It would require work to add that to the compiler. It would add work to hook it up to the IDE. There are different types of usings. using namespaces. using aliases, and extern aliases. There are also usings in nested namespaces, etc. That’s just the basic issues off the top of my head, and I’m sure I’m forgetting something. With a language as complex as C# there are always unforeseen scenarios that break your assumptions about how a feature works. Usually this affects the IDE the worst since it needs to be incredibly tolerant of user coding mistakes, but it also affects the compiler as well. It would also add to the QA cost and a require time to be allocated to this feature. This work would then decrease the amount of time we could work on other things and would affect performance of the rest of the product.

    However, i’m fairly sure you weren’t referring to performance in that way. You were probably referring to the performance of the compiler in general. In that way the answer is still "yes". While with a straight compile a user would probably not notice the overhead of checking this there are other problems that would arise that would affect performance. Say we were doing an incremental compile, in such a system you have to do a lot of bookkeeping all the time, and the logic to keep your data structures always up to date with the code. Tracking used and unsused "usings" is another bit of bookkeeping that you have to ensure consistency over. Because of that it might mean that optimizations in place are no longer valid, or that you might have to recompute far more than before on a change.

    However, like all features, it’s a tradeoff. It’s rare that you ever get anything for free and you are always trying to get the best value out of the software. that’s one reason i’m asking this question. If these notices are worth their cost, then we’ll do it 🙂

    Does that help answer the question?

  3. Roy Dictus says:

    Hi Cyrus,

    Great suggestion!!

    Yes, a static analysis feature built into the compiler or built into Visual Studio would definitely be appreciated. Now, of course tools such as FxCop can handle a lot of those situations, so maybe it would be easier to just integrate them with VS.NET as add-ins…

    What you’re suggesting is very similar to Lint utilities for C and C++. There’s a whole list of such tools at, but not a lot is available for .Net. DevPartner Studio from Compuware does some static analysis, as does VIL (the freeware utility at

    And as to the cost, obviously it should be a feature that you can turn on or off, just like the XML documentation generation etc.

    Many thanks,


  4. Dr Pizza says:

    Such messages should be emitted and should be warnings. They’re warning you that your code contains redundant elements. This is typically an indicator that you’ve forgotten something or improperly refactored something.

  5. Tobias says:

    How about warning levels, like C++? If some of the warnings could be switched off then I would like more of the stuff currently logged in FXCop to be generated by the complier.

  6. Tobias: Why would you want the FxCop stuff to be generated by the compiler if FxCop can do the checking for you?

    And you would definitely be able to turn these messages off 🙂

  7. Dr.Pizza: That’s one way to look at it. It would certainly be fine to classify these informational tidbits as low warnings.

  8. Roy: The cost is not returned by being able to turn this feature off or on. The cost was already incurred by taking away resources from our current set of work to be able to do this work. Each of those other features will then have not had much time put into them as before, and turning this off won’t bring that back.

  9. David Hayes says:

    I’ve been using Eclipse and recently and that spews out all sorts of usefull ‘warnings’ about code that might not be what you intended. Coming back to VS.NET I really miss the compiler not telling me about unsed variables etc.

    In short I think it’s a great idea

  10. Jim Argeropoulos says:

    I am game. I have been bitten by the property one already.

  11. I was thinking it’d be great for FxCop integration with the IDE so that I could have the FxCop rules run during compilation and the warnings would appear in the compiler output and Task list.

    But I’m using VS.NET 2003 and realized you can pretty much get that with VS.NET 2005 by adding FxCop as a compiler step since your project file is an MSBuild script. Though I don’t know if that will put FxCop warnings into the output window or Task list.

  12. Philip Rieck says:

    Yeah, these messages are useful, but I agree that these should be FXCop rules. You’d get the biggest return by integrating FXCop with the IDE as stated above. Then, not only do we get *all* of the static analysis currently done by FXCop, but we also get any rules that we want to write as well. Adding a rule doesn’t impact the IDE code base. And we don’t have to wait for the next version of VS to add more static analysis messages. win-win-win. (If it’s possible to add that type of integration, that is)

  13. Travis says:

    I like it. Especially the infinite loop one. Happened to me the other day. I went, "WTF? Stack overflow?! Ohh…haha. Whoops. That should be *lower* case." A "hey, are you sure this is what you mean" from the compiler would be appreciated.

  14. Ted Howard [MSFT] says:


    Static analysis is always welcome. The first question I ask anyone who rights in C++ is "Do you use warning level 4? Why not? Is that reason really valid?" The answers, of course, are "No. Too many messages. Well…"

    I’m glad to see that Whidbey will bring static analysis tools to a first-class status. Prefix, prefast, FxCop, … all of these are useful to ensure a certain code quality. And it look like Whidbey will be written to allow for custom static analysis tools, like those that many companies use internally.

    Yes, more warnings makes for better code.


  15. I really miss the "organize imports" from eclipse – ctrl+shift+o (iirc, whatever it was bound to) and my imports were automatically stripped of unnecessary things, needed ones were added (with prompting if it wasn’t clear which), ordered as I wanted them, etc.

    Don’t get me wrong, there are things that VS does better than Eclipse, of course, but a lot of things Eclipse does are in that category of "you don’t realize how much you miss it until its gone" 🙂

    I miss scrapbook too – the ability to just want to type in a little chunk of code and have it evaluate. That’s probably in VS somewhere and I just haven’t run across it yet 🙂

  16. Paul M says:

    Yes! Whether or not this feature gets put in the compiler, IDE, or FxCop, this feature *definitely* belongs in my workflow. It’s nice to have someone looking over my shoulder looking for brain hiccups. And it would help eliminate some of the drivel that gets checked in by hurried team members.

  17. Unused "usings" could be treated just like unused variables and generate a message, but I think in both cases the feedback comes too late. I like the way ReSharper handles it much better — the offending code is highlited in real-time. I also second the request for an "organize usings" feature or at least for a way to define how usings should be organized so that the IDE can put them in my preferred order when it auto-adds them as part of the new refactoring feature.

  18. James: Why would i get you wrong? I’m very well aware of the excellent work that eclipse does!! It’s pretty fantastic and is only going to get better. But hey, I wouldn’t find my work half as interesting if there wasn’t great competition going on in the industry.

    I thought I’d respond to the link you placed:

    "Control-Space: used for identifier completion, template insertion. Try typing "pub" then Ctrl-Space, or "for" then Ctrl-Space, or a prefix of one of your variables then Ctrl-Space. "

    In vs2k5. 🙂

    "Ctrl-F6: switches to another editor tab. It’s a stupid default – change it to Ctrl-Tab in Window/Preferences. "

    In 2k5 (and 2k3 i think)

    "Scrapbook pages. File/New/Scrapbook page. Type some Java code, SELECT IT, and then right-click to run it immediately (Inspect, Display, or Execute). Don’t use this to test your own code, since it makes you lazy. But it’s great for learning how to use somebody else’s code, like Java API classes. "

    Available in 2k5 through the new "Object Workbench"

    "Rename/move. Right-click on any package, class, method or variable in the Package Explorer and select Refactor/Rename to rename it or Refactor/Move to put it in another package or class. "

    Re have rename, but we don’t have "move" yet.

    No organize imports yet 🙁

    But code organization is something we’re really looking into

    "Project sets. File/Export/Team Project Set "

    2k5 Profiles

    "Source/Override Methods. Pops up a dialog box letting you pick which methods of the superclass you want to override, and inserts templates for them automatically. "

    We have that, but you can only do it one at a time, or all at a time.

    "Source/Generate Getter & Setter. Creates get/set methods for a field. "

    2k54 has that with the encapsulate field refactoring

    "Ctrl-1: fires up Quick Fix. Offers a range of instant fixes for each error message. "

    that’s ridiculously cool!!

    "Fast View. Drag any view window (not an editor) to the vertical toolbar on the left. It will drop there as an icon, so you can pop it open quickly, use it, and close it. "

    Interesting, like a dock for inside an application.

  19. Machael: I agree, having the option to highlight in real time is fantastic and is a sorely missed feature in the current VS offering.

  20. Josh says:

    Check out the C# Refactory add-in from It has a ‘tidy imports’ feature that can be run at the class, project, or solution level. Very cool. Oh, and it also has almost every known refactoring feature.

    Warning – we use the 1.20 version, not the 2.0 version, which blew up when we installed it on a couple of boxes.

    Not affiliated, just a huge fan of the product. Worth the c-note

  21. Darren Oakey says:


    Being the ultra-protectionist that I am – I always turn "blow up on warnings" on, run fxcop and everything and so on. As we have talked before, I would LOVE to have all that functionality on intellisense, but having it on compile is next best… but in both your examples:


    > value = value;


    > public int Property {

    > get {return Property;}

    Ideally, I would like an error message. While I understand that a) could actually be causing some action (a refresh or something), there is no excuse for that sort of code – that line SHOULDN’T exist ANYWHERE… force people to write myObject.Refresh() – And in the second one – well – while you might have some tricky exception handler or something looking at the stack trace, in the 1 out of 1000 times you would do something like this intentionally it would be psychotic programming, in the other 999 times it would be a typo.

    I’d prefer the compiler to be utterly limiting in what it allowed, forcing you to occasionally do a dodgy to get around the limitations. Reason being – MOST of the time that sort of behaviour in your compiler is helping you, and the few times you are forced to get around it, you are THINKING about it.

    So, I’m all for adding every sort of best practice, best code, pattern detection, syntax checking, comment checking, spell checking, style checking, metric checking, cyclomatic complexity checking to the intellisense, or at worst, compile process – and have it blow up if ANYTHING is even slightly out of place.

    because your average programmer, in your average team isn’t the sort of person sitting here reading these blogs. Your average programmer only vaguely understands what they are doing, and likely as not has found something that they thought looked similar to the task they’ve been told to do, copied the code in, and is trying to modify it to work like they want. I think you’ll find a lot more people code like that than any other method… and it’s scary. We should put every possible impediment to making mistakes we can in their way, and also force them with lots of breaking messages to THINK about what they are doing!

  22. Darren: I agree with you 🙂

    I don’t have VS in front of me, but if I’m not mistaken you should be able to turn these warnings into errors. Something like the /we flag for C++

    If we don’t have that, then we definitely should add it. For our own code base we have "treat all warnings as errors" turned on because we’re fairly certain that if we ever get one of those warnings warnings chances are it will eventually turn into a bug later on. There are some warnings that are annoying, but usually reworking the code in those cases ends up with code that’s actually cleaner and more maintainable.

  23. Valentin says:

    ReSharper ( already does pretty much of the stuff mentioned above (live errors/warnings highlighting including unused usings, removing unused usings, refactorings, etc).

  24. Valentin: Thanks for the information!

  25. Basically, YES YES YES! Do this. Code purists would love it, and to be honest, anything that helps me reduce the size of my code is a good thing. Of course, I understand the arguments about what it would cost to do it, but that is a decision for you guys to make. I can only say that the customer definitely would want it, and it would be beneficial. The more information you can give us about our code, the better.

Comments are closed.

Skip to main content