n this workshop, we will cover the basics of C# (data types, control flow statements, inheritance) and some more advanced concepts (fancy collections, event handling, linq expressions). We will be working with examples in Unity 3d.
It is intended for participants at a beginner or intermediate level. You should have a basic knowledge of C# + Unity3D or do Aurelie’s introductory workshop beforehand.
2. Primitive Types
Primitive types represent simple values
Description Signed Unsigned
Boolean (true or false) - bool
Characters (such as 'a', '1', '?') - char
Bytes (8-bit) sbyte byte
Short integer (16-bit) short ushort
Integer (32-bit) int uint
Long integer (64-bit) long ulong
Floating-point (32-bit) float -
Double precision floating-point (64-bit) double -
3. Reference Types
Reference types are classes
A program creates instances (or objects) of
classes
It keeps references to these instances in
variables
When an instance has no more reference to it, it
will eventually be destroyed by the garbage
collection.
null is a special value that represents a reference
to nothing
4. Classes
Classes have 4 kinds of members:
Fields (the data): variables kept in the instance
Methods (the code): actions that the code can
perform
Properties (some more code): they're used like fields
with some extra logic around the manipulation
Events: a way for the code to register/unregister
callbacks to respond to events
5. Inheritance
Classes can derive from others
Every class implicitly derives from System.Object
Virtual, override modifiers
ToString(), GetHashCode(), Equals()
6. Access Modifiers
private: can only be accessed by this class
protected: can only be accessed by this class
and its derived classes
internal: can be accessed from anywhere in the
same project (or assembly)
public: can be accessed from anywhere
static: associate the member with the type
instead of the instance
Static members don't have access to non-static
members
Non-static members have access to static members
7. Some Special Types
string: this is a class containing a collection of
characters.
enum: an enumeration is a list of words
associated with int values. It is a very convenient
way to give meaningful names to constant values,
make the code easier to read.
struct: structures are like class but are handled
by value, not by reference. They're useful for small
amount of data, such as vectors or matrices.
10. Method Arguments
Regular args.
The caller
passes a copy
of the value to
the callee
If the callee
modifies the
argument, it
only modifies
the copy, not
the original
ref args
The caller
passes a
reference to the
value
If the callee
modifies the
reference, it
also affects the
original
out args
Same as a ref
argument
The caller
doesn't need to
initialize it
The callee has
to write it
It's like an
extra return
value
11. Collections
Arrays: static, items cannot be added or removed
Lists: dynamic, items can be added and removed
Dictionaries: key-value storage. Values are
accessed by key instead of indices
Sorted Collection: each collection has a sorted
version to allow faster searches (but slower
insertions and removals)
12. Events
Events are useful when many objects need to be
notified when something is happening.
The naive approach: the emitter explicitly calls all the
interested objects
The fancy approach: all the interested objects
register to the emitter, which will fire its event that will
be propagated to the registered ones.
13. Lambda Expressions
Very convenient way to add small anonymous
functions within other functions
Can make the code harder to read when
overused
14. Linq Expressions
Convenient operations on collection
Conversion
Filtering
Transforming
Counting
Merging
Intersecting
…
Have a small performance penalty
Can make the code hard to read
15. Some Tips for the Road
Keep it simple! Don't over engineer, keep the
code as complex as the task it performs, not
more.
Naming things is difficult, but important. Code is
written once, but read many times. Spend a little
more time finding good names, it will pay back.
Organize your code in a way that will make it
easy to retrieve your stuff.