Upload
pixelles
View
233
Download
0
Embed Size (px)
Citation preview
Intro ++ to C#
1.Primitive Types2.Reference Types3.Special Types4.Casting5.Collections6.Event7.Lambdas8.Linq
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 -
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
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
Inheritance
Classes can derive from others Every class implicitly derives from System.Object Virtual, override modifiers ToString(), GetHashCode(), Equals()
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
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.
Control Flow Statements
Branching If, else Switch, case
Loops For Foreach While Do, while
Break, continue and goto
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
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)
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.
Lambda Expressions
Very convenient way to add small anonymous functions within other functions
Can make the code harder to read when overused
Linq Expressions
Convenient operations on collection Conversion Filtering Transforming Counting Merging Intersecting …
Have a small performance penalty Can make the code hard to read
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.