Posts Tagged properties

C# APIs are chock-full of generics. Generic types, generic method parameters, generic return types, generic fields, generic properties, deriving from generic types, and generic constructors. We can find all of these in the Unity and .NET APIs. Some are more frequent than others, but we’re going to need support for all of them to make C++ scripting a viable alternative to C#. Today’s article continues the series by adding just that: support for all of these kinds of generics. Let’s dive into how to use them as well as some bonus items added to the project this week.

C# has properties similar to AS3’s get and set functions. These functions can even be auto-generated for you, which is very convenient. However, the auto-generated versions don’t expose the so-called “backing field” that the property gets and sets. This brings up a question: is there a performance penalty to using an auto-generated property rather than manually implementing the property so we can directly access the backing field? The get and set blocks are like functions, so are we paying function call overhead for them every time we access our own private pseudo-variables? Finally, could we do even better by skipping fields altogether and working on local variables instead? Today’s article puts all three approaches to the test by analyzing the bytecode that’s generated and the performance within a Unity test environment. Read on to see which way is fastest!

Picking up from last time, today we’ll finish off classes in C# from an AS3 perspective in preparation for next week when we delve into all-new concepts that aren’t in AS3 at all. Read on to learn the C# way to implement getters and setters, final functions and classes, const variables, and packages.

Quite often I have wanted to iterate over the public fields (and getters) of an arbitrary object. Sometimes this is for debugging purposes so I can print out the state of an object, particularly one that has all public fields like a C/C++ structure. Sadly, this is not possibly (with most objects) using the for-in and for-each loops we’ve come to know and love. So I made a utility function that I’m sharing with you all today. UPDATE: getProperties now supports MovieClip derivatives such as library symbols from an FLA