Get int value from enum in C#

Just cast the enum, e.g. int something = (int) Question.Role; The above will work for the vast majority of enums you see in the wild, as the default underlying type for an enum is int. However, as cecilphillip points out, enums can have different underlying types. If an enum is declared as a uint, long, … Read more

Downcasting in Java

Downcasting is allowed when there is a possibility that it succeeds at run time: Object o = getSomeObject(), String s = (String) o; // this is allowed because o could reference a String In some cases this will not succeed: Object o = new Object(); String s = (String) o; // this will fail at … Read more

Why use static_cast(x) instead of (int)x?

The main reason is that classic C casts make no distinction between what we call static_cast<>(), reinterpret_cast<>(), const_cast<>(), and dynamic_cast<>(). These four things are completely different. A static_cast<>() is usually safe. There is a valid conversion in the language, or an appropriate constructor that makes it possible. The only time it’s a bit risky is … Read more

casting void** to 2D array of int – C

The compiler is right, an array of arrays (or a pointer to an array) is not the same as a pointer to a pointer. Just think about how they would be laid out in memory: A matrix of size MxN in the form of an array of arrays: +————–+————–+—–+—————-+————–+—–+——————+ | matrix[0][0] | matrix[0][1] | … … Read more