Archive for category C#

Making Enums More Flexible and Extensible

Tags: , ,

Enums are great at what they do: creating a simple integer type with specific values. Their main purpose is to choose one value out of many like enum Color { Red, Green, Blue }. But what if you have data attached to those choices? What if the data is one type for one choice and another type for another choice? What if there are two pieces of data to attach to one choice and only one for another? Today’s article shows a simple pattern you can use instead of enum in these cases to get a lot more flexibility and extensibility. Read on to see how!

Read the rest of this article »

5 Comments

Delegate() vs. Delegate.Invoke()

Tags: , ,

In reading others’ C# code I consistently see some programmers call delegates like a function—del()—and others use the Invoke method of the Delegate class: del.Invoke(). Is there any difference between the two? Is one better than the other? Today’s article finds out!

Read the rest of this article »

9 Comments

Enum Arithmetic and Operators

Tags: , ,

C# enum types are an easy and efficient way to make an integer type without all the overhead of something like a class or even a struct. They’re basically a synonym for an integer type like byte or int. However, that “basically” hides a lot of details that affect the way you can work with them. Today’s article explores the arithmetic and operators you are and aren’t allowed to use when you opt for an enum over an int so you’ll have a better understanding of how and how not to use them.

Read the rest of this article »

No Comments

Comparing Null Objects

Tags: , , ,

We all use <, <=, >, and >= with integers and floating point values all the time. It just works and it’s built into basically every programming language. These simple operators suddenly become quite a pain when you start wanting to compare other objects. IComparable seems to make it easier, but there’s some trickiness when you start dealing with null objects. Today’s article explores this and ends up with some handy utility functions to take some of the gotchas out of comparing.

Read the rest of this article »

No Comments

Do Events and Delegates Create Garbage?

Tags: , , , ,

Unity’s garbage collector can be disastrous to our games’ framrates when it runs so we’d best not incur its wrath. We’ve seen that foreach loops usually create garbage, so the natural followup question is “what other language features create garbage?” Events and delegates are extremely handy features of C#. They serve as the function pointers and Function objects of the language. They replace signals and slots and allow for flexible callbacks. But a lot of what they do is behind the scenes. Are they creating garbage back there? Today’s article puts them to the test to see if creating and calling delegates and events creates any garbage. Read on to find out!

Read the rest of this article »

16 Comments

Using Structs to Avoid Creating Garbage

Tags: , , ,

It’s easy to forget about struct in C#. After all, it’s not available in other languages like Java or AS3 and it seems to have fewer features than good old class. But struct can really help you out when it comes to garbage creation! Today’s article discusses some strategies to get the most out of struct. Read on to learn how to use structs to put a stop to that pesky garbage collector!

Read the rest of this article »

8 Comments

Do Foreach Loops Create Garbage?

Tags: , , , , ,

We know that we should reduce the garbage our code produces to lighten the load on Unity’s garbage collector. The trouble is that many of the ways we’re creating garbage are hidden from us. One such way to inadvertently create a lot of garbage is to use a foreach loop… at least that’s what we’ve been told. Do foreach loops really create garbage for all types of arrays, lists, dictionaries, and the rest of the collections? Do they create garbage for every loop or just the first one? Today’s article investigates to put these questions to rest. Are you safe using foreach loops or should you re-write everything to use for. Read on to find out!

Read the rest of this article »

8 Comments

Using Object Pooling to Reduce Garbage Collection

Tags: , ,

Unity’s garbage collector is old and slow. As garbage from your scripts piles up, the garbage collector will eventually run. When it does, it runs all at once instead of spreading the work out over multiple frames. This means you get a big spike on one frame causing your game to momentarily freeze. One of the best ways to get around this is to use an “object pool” to reduce the number of objects eligible for garbage collection. After all, if there’s no garbage then there’s nothing to collect! For more on this strategy as well as a class you can use to implement it, read on!

Read the rest of this article »

10 Comments

Too Many Coroutines: A Queue Solution

Tags: , , ,

Unity’s coroutine support is great. So great that it’s easy to go overboard and end up with too many of them. That could be for any number of reasons. Perhaps the coroutines are using too much memory or have too many files open at once. In any case, you’ll need to find a way to limit how many are running at a single time. Today’s article introduces a solution to the problem that queues coroutines so you never have too many running. Read on to learn about the solution and for the class that implements it.

Read the rest of this article »

19 Comments

How to Recover Anonymous Types

Tags: , , , ,

When we just need a quick and dirty type to hold some values, C#’s anonymous types fit the bill: var person = { First="John", Last="Doe", Age=42 }. On the down side, since these types are anonymous they have no explicit type. The var variable is strongly typed, but you have to use the object type when passing them to other functions. But then how do you get the fields back out? Today’s article shows you how so that anonymous types will be more useful to you. Read on to find out how to recover anonymous types!

Read the rest of this article »

No Comments