Dana Vrajitoru
B583 Game Programming and Design
Introduction to C#
C#
- C# is an object-oriented language developed by Microsoft
especially for platform games and apps.
- Currently one of the two options for Unity scripts. The other one
is Javascript.
- Strongly typed, almost every type is a class, functional (has
anonymous functions).
- No explicit pointers but many things are pointers.
- Very similar to C++ and Java. A lot of common syntax.
Data Types
- Simple C-style data types are available: int, float, double,
char, bool, etc. OOP-style function calls can be made on them.
- int i;
i.ToString()
- Everything else is either a class, a struct, or an array of one of
these data types.
- Function prototypes (or signatures) can be grouped in an
interface.
- Value types (simple, struct) vs reference types (classes).
C# Classes
- Single inheritance from another base class.
- Attributes and methods can public, protected, or private (or
internal).
- Static attributes or methods belong to the class. All the others
are instance-based.
- The function Main() must be static if present in
a class.
- Classes can inherit from multiple interfaces.
- Multiple constructors allowed.
- Syntax similar to C++.
Classes in C#
public class Manager: GameObject {
public int level;
public Manager() {
level = 0;
}
...
}
Manager m = new Manager();
Structs
- Similar to C++.
- Similar to classes: they have attributes, methods, constructors.
- They don't inherit from other structs or classes.
- No user-defined default constructors. A struct constructor must
provide values for every field.
Example
public struct CoOrds {
public int x, y;
// cannot initialize them here
// unless they are static or in Unity
public CoOrds(int p1, int p2) {
x = p1;
y = p2;
}
}
Arrays
- No static size for arrays. They must be initialized with the new
operator.
- int[] a1 = new int[5];
- int[] a2 = new int[] { 1, 3, 5, 7, 9 };
- int[] a3 = { 1, 2, 3, 4, 5, 6 };
- Multidimensional arrays:
- int[,] ma1 = new int[2, 3];
- int[,] ma2 = { { 1, 2, 3 }, { 4, 5, 6 } };
- Access: ma[i, j];
Loops
- Usual C++ loops: do, for, while.
- foreach loop: iterates over an array or some other
collection.
foreach (int element in a1) {
System.Console.WriteLine(element);
}
for (int i=0; i < a1.Length; i++) {
element = a1[i];
System.Console.WriteLine(element);
}
Exception Handling
- try { } - enclose a piece of code that might have an
error.
- catch(ExceptionType e) { } - action to be taken in case
the specified error happens.
- throw new ExceptionType(); - generate an exception of
this type that can be matched to a catch.
- ExceptionType must be derived
from System.Exception .
- throw; - with no argument inside a catch, throws an
exception of the same type.
Example
object o2 = null;
try
{
int i2 = (int) o2; // Error
}
catch (InvalidCastException e) {
// take some action
}
Function Parameters
- Unless specified otherwise, a parameter is a value parameter
(except that objects are references).
- A parameter preceded by ref is a reference parameter. It
must be initialized before the function is called.
- A parameter preceded by out is an output reference
parameter. It must be initialized inside the function.
Ref Example
class RefExample
{
static void Method(ref int i) {
i = i + 44;
}
static void Main() {
int val = 1;
Method(ref val);
Console.WriteLine(val);
// Output: 45
}
}
Out Example
class OutExample {
static
void Method(out int i) {
i = 44;
}
static void Main() {
int val;
Method(out val);
Console.WriteLine(val);
// Output: 44
}
}