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.
First up are getters and setters. As a refresher, here’s how they look in AS3:
Now let’s see the equivalent code in C#:
Instead of being declared as two functions, the getter and setter are declared as a single variable. That variable doesn’t end with a semicolon (
;
) but instead a curly braces block containing to parts: get{}
and set{}
. Those parts are the getter and setter functions. Inside the setter you get access to a new keyword: value
. This means “the value to set” because you never declared it as a parameter, unlike AS3. Other than this syntactical difference, everything else works the same.
C# also provides a shorthand to avoid most of this typing for simple getters and setters. Behold:
Using this will automatically create the
_name
variable and the getter and setter functions just as they were in the previous example. But what if you didn’t want a setter? Just leave it out:
However, you probably want at least the
Person
class to be able to set it. In that case, just declare it private:
Finally, one bit of terminology. The field of the class that does the getting and/or setting (
Name
) is called a “property” in C# lingo. The variable it gets and sets (_name
) is called the “backing field”.
Next let’s talk about “final” functions that can’t be overridden.
C# has the same concept with its
sealed
keyword again:
Now on to packages in AS3:
C# simply calls a package a namespace instead:
To give access to the classes in a package to a
.as
file, you import
the package:
In C#, you say that you’re
using
the namespace:
Finally today, let’s talk about
const
fields in AS3. This is how you make a field that can’t be changed after it’s declared:
Here’s how it’d work in C#:
One important difference is that in C# you can only make basic types (e.g.
int
) and strings const
. This is because the field will not actually exist. Instead, it’s erased and inlined everywhere that it’s used. This means that it’s inherently a static
variable, so you don’t need to declare it as static
. The Person
class in C# will actually be compiled like this:
In AS3, the
const
keyword is just for compile-time checking and won’t result in any inlining. If you want that behavior in C# or need to use non-basic types (e.g. Person
), you need to use the readonly
keyword instead:
Here we see two
readonly
fields. The Newborn
field is declared static
since readonly
fields are not necessarily so. It’s initialized where it’s declared just like you must do with const
fields in AS3. However, the Age
field is readonly
but not static
. It’s initialized in the construct, where it must be initialized in C#.
Lastly, here’s a comparison between C# and AS3 covering everything in this article:
No comments:
Post a Comment