Hacker News new | past | comments | ask | show | jobs | submit login

Since we're all chipping in with features we'd like to see in C#, here are mine: anonymous classes - with actual class bodies, that can implement interfaces - and traits.



Yeah, coming from Java a decade ago this was the first thing I noticed lacking. And with all the stuff C# has introduced beyond that, I'm always fairly surprised that this hasn't been done.

In preparation, the feature currently called "anonymous classes" in C# needs to be redubbed "anonymous records". And in preparation for that, well, "nonymous" records need to be introduced.


Can you give an example of how C# would be different with anonymous classes?

Googling that term just gives a whole lot of references to inner classes in Java.


Implementation of IComparer


One I've come across is binding a command to a button. With anonymous inner classes you can have something like:

  myButton.AddListener(new Command {
    bool enabled() {
      return false; //more logic goes here
    }
    void onClick() {
      //do something
    }
  });


But that's something that's very rare in C#. Usually for that sort of thing you have events and delegates. Although I have to admit that the library I work on had a few patterns that would benefit from anonymous interface implementations, but not that much to really miss the feature.


much much better to use DelegateCommand:

    SubmitCommand = new DelegateCommand(()=> DoSomething(), ()=> IsSubmitEnabled);
Edit: and obviously the Command DependencyProperty on the Button will be bound to the SubmitCommand property, instead of accessing directly the button from the ViewModel, that is the worst possible thing that you can possibly do.


I'm not convinced that gives you that much more over using a delegate.


It doesn't. It's a nice to have feature, not a world changing one.


You could, if you were so inclined, just generate proxies on the fly.




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: