Translation of the article From AS3 to C #, Part 1: Class BasicsThis article will help you understand the basics of C # to those AS3 developers who decide to switch to Unity, but are completely unfamiliar with C # programming.
Let's start with the main thing: C # files have the extension .cs, and not .as as AS3 files. Inside the files you will find a description of the classes that the applications consist of (similar to AS3 files). For example, the file MyClass.cs:
')
class MyClass { }
The class names in C # follow the same convention as the class names in AS3: each word in the class name must begin with a capital letter, the rest of the name is written in lower case. As you can see, the basic syntax for classes in C # is the same as for classes in AS3.
Now let's add a variable:
class MyClass { int X; }
Defining variables in C # is different from AS3. Instead of var x: int, you must use int X. The convention of names says that variable names must begin with a capital letter.
Let's look at the functions:
class MyClass { void Foo() { } }
Functions in C # are also different from functions in AS3. In C #, the function keyword is not used; the returned type is given at the beginning (and not at the end, as it was in AS3), the name of the functions must begin with a capital letter, based on the convention of names.
Function with input parameters:
class MyClass { void Foo(int x, int y, int z) { } }
The parameters of the functions, as well as in AS3, are inside the brackets, but they are written a little differently: first, the type, then the name, with a space between them. The convention says that function parameter names should begin with a small letter.
As in AS3, function parameters can have default values:
class MyClass { void Foo(int x = 1, int y = 2, int z = 3) { } }
Function constructor:
class MyClass { MyClass() { } }
As in AS3, we do not have to specify the type of the return value for constructor functions. In fact, you cannot even specify the void data type, as it could be done in AS3. Also, to declare this type of function, the function keyword is not used.
You can have more than one constructor function, this is called function overloading:
class MyClass { MyClass() { } MyClass(int x) { } MyClass(int x, int y, int z) { } }
A reboot is also available for other functions:
class MyClass { void Foo() { } void Foo(int x) { } void Foo(int x, int y, int z) { } }
In order for the compiler to determine which function is called, you need to follow 2 rules. First, you can not have 2 functions with the same parameters:
class MyClass { void Foo(int x, int y, int z) {
The same applies to those functions whose call may look the same if one of the parameters is not specified by default:
class MyClass { void Foo(int x, int y, int z) {
Secondly, you cannot “overload” functions by the type of data they return:
class MyClass { int Foo(int x, int y, int z) { return 0; } uint Foo(int x, int y, int z) { return 0; } }
Adhering to these two simple rules, you can “overload” the functions as your soul desires =)
As in AS3, to define static functions, you must add the keyword static:
class MyClass { static void Foo() { } }
The same with static variables:
class MyClass { static int X; }
Now that we know how to define classes, variables, functions, and constructors inside classes, let's consider how to use them:
void SomeCode() {
All this is very similar to AS3. Note that the local variable mc begins with a small letter, as well as the parameters of the functions.
Let's discuss access modifiers. Standard modifiers public, private and protected have the same syntax and the same behavior as in AS3. To use them, simply add them before the returned data type of the function, variable type, or class keyword:
public class MyClass { private int X; protected void Foo() { } }
In C #, as in AS3, there is an access modifier internal, which has the same name, but slightly different behavior (as compared to AS3). In AS3, the internal modifier means "available for the current class and other classes in the current package." In C #, it means "available for the current class and the rest of the classes in the current assembly." I will talk about the concept of “assembly” in other articles, but for now, think of them as a group of classes compiled in one binary file.
Also, in C # there is a protected internal modifier, which is a combination of protected and internal.
Finally, a comparison of the described features of C # and AS3 code:
This completes the first part of the “C # for AS3 Developers” article series. The following articles will deal with more complex topics, including such C # features that have no analogs in AS3.
Stay in touch!