Tagc++

Java Enums in C#?

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:

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.

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.

Moving towards the first playable

First moving units around their bases

First moving planes around islands. Stay tuned. :)

c++ arc4random on android, windows and linux

Turns out native android development doesn’t supply you with a real ar4random method. However adapting the OpenBSD’s arc4random.c does the trick. And even works on windows without using CryptoAPI (thanks to Chris K. Jester-Young for these suggestions). Additionally with boost *ducks* it should be more or less multithreadsafe.

Mixed (red dots are generated by arc4; Math.Random() Arc4random)

And here are the convinience methods for it:

So you can use it like:

Enjoy. (:

See also

Merging and splitting two ints in c++

In c++ an int consists of 4 bytes and can hold a max value of 2147483647 (2^31-1). Should you ever feel the obscure need to merge two ints, because you know that both of your values will never be blow 0 or above 32767 (2^15-1) use the following methods.

1) Merging

2) Splitting

3) applying in a test method

Conclusion:

c++ empoweres me to do probably unnecessary and weird stuff.