I really love C#! Almost all features like delegates, extensions methods, constraints, lambdas, refs, “magic methods”, etc. are really super convinient to use. It’s totally my favorite language so far and trust me I’ve been around with C/C++, Php, Javascript, Java. I pretty much code all the time in C# nowedays.
Probably one feature that Java did way better than C# is the way to extend enums. As stated on this website especially made for java to c# converting people.
Enums are used to create and group together a list of user defined named constants. Although on the surface the enumerated types in C# and Java seem quite similar there are some significant differences in the implementation of enumerated types in both languages. In Java, enumerated types are a full fledged class which means they are typesafe and can be extended by adding methods, fields or even implementing interfaces. Whereas in C#, an enumerated type is simply syntactic sugar around an integral type (typically an int) meaning they cannot be extended and are not typesafe.
You can use enums in Java as collection of concrecte behaviour implementations:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | public interface IStringFilterCommand { public boolean filter(@NotNull final String source, @NotNull final String searchString); } public enum StringFilterCommand implements IStringFilterCommand { IS() { @Override public boolean filter(@NotNull final String source, @NotNull final String searchString) { return source.matches(searchString); } }, CONTAINS() { @Override public boolean filter(@NotNull final String source, @NotNull final String searchString) { return source.contains(searchString); } }, CONTAINS_NOT() { @Override public boolean filter(@NotNull final String source, @NotNull final String searchString) { return !source.contains(searchString); } }, STARTS_WITH() { @Override public boolean filter(@NotNull final String source, @NotNull final String searchString) { return source.startsWith(searchString); } }, ENDS_WITH() { @Override public boolean filter(@NotNull final String source, @NotNull final String searchString) { return source.endsWith(searchString); } }; } IStringFilterCommand filter= getFilterFromSomewhere(); filter.filter("blakeks", "bla"); or directly StringFilterCommand .STARTS_WITH.filter("blakeks", "bla"); or with static import: STARTS_WITH.filter("blakeks", "bla"); |
Or have a concrecte amount of certain Objects of a kind like a represenation of different resolutions.
Sure you can do it with normal classes, readonly attributes in c# too. But i was wondering how much of the functionality you could “port” towards c# with the help of generics, extension methods and injected attributes. So this is a small test i came up with the help of some respective stackoverflow articles.
1 | c# javaenum test |
Conclusion
Well it’s far from elegant/readable or even practically usable. However it’s possible to a certain extend. Feel free to correct me.