This article is for the AS3 developer who’s decided to switch to Unity and doesn’t know the first thing about programming in C#. It’ll walk you through the basics of C# to get you oriented and productive.

Table of Contents

First things first, C# files have the file extension .cs instead of AS3’s .as. Inside them you find the class definitions that make up your app’s code, just like with AS3. For example, here’s a MyClass.cs file:

class MyClass
{
}

Classes in C# have the same naming convention as with AS3: capitalize each word but leave the rest as lower-case. The actual class syntax is the same too, as you’ve likely noticed. Now let’s add a variable field:

class MyClass
{
	int X;
}

As you can see, variable fields are declared differently in C#. Instead of var x:int, you use int X. The naming convention says to capitalize public field variable names and lowercase the rest. I’ll talk about access specifiers more in a later article. For now, assume everything is public. Now let’s see a function:

class MyClass
{
	void Foo()
	{
	}
}

Functions are also different. You don’t use a function keyword, the return type comes at the beginning instead of the end, and the naming convention says to capitalize the function name. Here’s a function with some parameters:

class MyClass
{
	void Foo(int x, int y, int z)
	{
	}
}

The variables go in between the parentheses—just like AS3—but they’re declared like field variables: type name. However, these variables are different from field variables in naming since you start them with a lowercase letter. Just like AS3, you can have default parameters:

class MyClass
{
	void Foo(int x = 1, int y = 2, int z = 3)
	{
	}
}

Next up is a constructor function:

class MyClass
{
	MyClass()
	{
	}
}

Just like with AS3, there’s no need to declare a return type. In fact, you’re not allowed to give it a void return type like you can with AS3. You also don’t use the function keyword. However, you can have more than one constructor function. This is called “overloading” the function and each of them is referred to as “an overload”.

class MyClass
{
	MyClass()
	{
	}
 
	MyClass(int x)
	{
	}
 
	MyClass(int x, int y, int z)
	{
	}
}

Overloading is allowed on other functions, too:

class MyClass
{
	void Foo()
	{
	}
 
	void Foo(int x)
	{
	}
 
	void Foo(int x, int y, int z)
	{
	}
}

There are a couple of caveats to “overloading” to keep in mind. The compiler needs to be able to figure out which function you’re calling, so two rules are put in place. First, you can’t have two functions with the same parameters:

class MyClass
{
	void Foo(int x, int y, int z)
	{
		// do some stuff
	}
 
	void Foo(int x, int y, int z)
	{
		// do some different stuff
	}
}

This includes functions that would be the same if the caller left out one or more default parameters:

class MyClass
{
	void Foo(int x, int y, int z)
	{
		// do some stuff
	}
 
	void Foo(int x, int y, int z, int w = 0)
	{
		// do some different stuff
	}
}

The other rule is that you can’t “overload” the functions by their return type:

class MyClass
{
	int Foo(int x, int y, int z)
	{
		return 0;
	}
 
	uint Foo(int x, int y, int z)
	{
		return 0;
	}
}

Keep these two simple rules in mind, and you can overload to your heart’s content.

Within a class you can use this just like in AS3, but not within static functions. Just like in AS3, you just need to add the static keyword:

class MyClass
{
	static void Foo()
	{
	}
}

Static class variables are the same way:

class MyClass
{
	static int X;
}

Now that we’ve seen how to declare a class, and variables, functions, and constructor functions in a class, let’s see how to use them:

void SomeCode()
{
	// Call the default constructor (i.e. no arguments)
	MyClass mc = new MyClass()
 
	// Call a different constructor
	mc = new MyClass(5);
 
	// Call a function
	mc.Foo();
 
	// Call an overloaded function
	mc.Foo(1, 2, 3);
 
	// Call a static function
	MyClass.Foo();
 
	// Get a field variable
	mc.X;
 
	// Set a field variable
	mc.X = 1;
 
	// By the way, single-line comments are the same...
 
	/*
	... and so are multi-line comments
	*/
}

All of that should look extremely similar to AS3. One detail is that the local variable mc starts with a lower case letter just like the function’s parameters.

Now let’s discuss access modifiers. The normal public, private, and protected modifiers are just like in AS3 and have similar syntax. Just add it before the return type of a function, type of a field variable, or class keyword of the class:

public class MyClass
{
	private int X;
 
	protected void Foo()
	{
	}
}

There is also an internal access modifier, which has the same name but a slightly different meaning than in AS3. In AS3, internal means “only accessible by this class and other classes in the same package“. In C#, internal means “only accessible by this class and other classes in the same assembly“. I’ll cover assemblies at some later date, but think of them for now as all the classes compiled together into one binary. Lastly, there is a protected internal access modifier which is a combination of both protected and internal.

Lastly, here’s a comparison between C# and AS3 covering everything in this article:

////////
// C# //
////////
 
/////////////////
// Declaration //
/////////////////
 
// Class
public class MyClass
{
	// Field variable
	public int X;
 
	// Class variable
	static public int Y;
 
	// Default constructor
	public MyClass()
	{
	}
 
	// Overloaded constructor
	public MyClass(int x)
	{
	}
 
	// Instance function
	public void Foo()
	{
	}
 
	// Overloaded instance function
	public void Foo(int x)
	{
	}
 
	// Class function
	static public void Goo()
	{
	}
 
	// Overloaded class function
	static public void Goo(int x)
	{
	}
}
 
///////////
// Usage //
///////////
 
// Call the default constructor
MyClass mc = new MyClass()
 
// Call a different constructor
mc = new MyClass(5);
 
// Call an instance function
mc.Foo();
 
// Call an overloaded instance function
mc.Foo(1);
 
// Call a static function
MyClass.Goo();
 
// Call an overloaded static function
MyClass.Goo(1);
 
// Get an instance variable
mc.X;
 
// Set an instance variable
mc.X = 1;
 
// Get a class variable
MyClass.Y;
 
// Set a class variable
MyClass.Y = 1;
 
// Single-line comment
 
/*
Multi-line comment
*/
/////////
// AS3 //
/////////
 
/////////////////
// Declaration //
/////////////////
 
// Class
public class MyClass
{
	// Field variable
	public var x:int;
 
	// Class variable
	static public var y:int;
 
	// Default constructor
	public function MyClass()
	{
	}
 
	// Overloaded constructor
	// {not supported}
 
 
 
	// Instance function
	public function foo(): void
	{
	}
 
	// Overloaded instance function
	// {not supported}
 
 
 
	// Class function
	static public function goo(): void
	{
	}
 
	// Overloaded class function
	// {not supported}
 
 
}
 
///////////
// Usage //
///////////
 
// Call the default constructor
var mc:MyClass = new MyClass()
 
// Call a different constructor
mc = new MyClass(5);
 
// Call an instance function
mc.foo();
 
// Call an overloaded instance function
// {not supported}
 
// Call a static function
MyClass.goo();
 
// Call an overloaded static function
// {not supported}
 
// Get an instance variable
mc.x;
 
// Set an instance variable
mc.x = 1;
 
// Get a class variable
MyClass.y;
 
// Set a class variable
MyClass.y = 1;
 
// Single-line comment
 
/*
Multi-line comment
*/

This concludes the first part of the From AS3 to C# series. Upcoming articles (starting next week) will cover more advanced topics, including features that don’t exist at all in AS3. Stay tuned!

Continue to Part 2

Spot a bug? Have a question or suggestion? Post a comment!