ITF11006.NET The Basics
Data Types – Value Types – Reference Types Flow Control – Conditional – Loop Miscellaneous – Enumerations – Namespaces Class / Struct – Data Members – Functional Mem Base Classes – System.Object – System.String
Variables/Constants Syntax [modifier] type identifier [= expression]; Scope – Class/Block Constants [modifier] const type identifier [= constant-expression]; Type Inference var identifier [= expression];
Predefined Data Types Value Types Reference Types
Predefined Data Types Value Types – Integer Types – Floating Point Types – Decimal Type – Boolean Type – Character Type
Integer Types C# data typeDescriptionClass nameRange sbyteA 8-bit signed integer.System.SByte-128:127 shortA 16-bit signed integer.System.Int : intA 32-bit signed integer.System.Int : longA 64-bit signed integer.System.Int : byteAn 8-bit unsigned integer.System.Byte0:255 ushortA 16-bit unsigned integer.System.UInt160:65535 uintA 32-bit unsigned integer.System.UInt320: ulongA 64-bit unsigned integer.System.UInt640:
Floating Point C# data typeDescriptionClass nameSignificant fig.Range floatA single-precision (32-bit) floating-point number. System.Single7+1.5x to +3.4x10 38 doubleA double-precision (64-bit) floating-point number. System.Double15/16+5.0x to +1.7x10 308
Decimal Type C# data typeDescriptionClass nameSignificant fig.Range decimalA 96-bit decimal value. (128 bit space!) System.Decimal28+1.0x to +7.9x10 28
Boolean Type C# data typeDescriptionClass nameRange boolLogical true or falseSystem.Booleantrue or false
Character Type C# data typeDescriptionClass name char16 bits. Represents a single (Unicode) characterSystem.Char
Nullable Types ( T? or System.Nullable(Of T) Ordinary type plus null
Predefined Reference Types Object String
Flow Control Conditional – if – switch Loop – for – while – do..while – foreach Jump – goto – break – continue – return
Conditional Statements if if (expression) statement1 [else statement2] switch switch (expression) { case constant-expression: statement jump-statement [default: statement jump-statement] }
Loops for for ([initializers]; [expression]; [iterators]) statement while while (expression) statement do..while do statement while (expression); foreach foreach (type identifier in expression) statement
Jump Statements goto break continue return (throw)
Enumerations enum [modifiers] enum identifier [:base-type] {enumerator-list} Inherits from System.Enum Flags Attribute Use enumerations
Namespaces Logical grouping of functionality. Using directive
Class (/Struct) Data Members – Fields – Constants – Events Function Members – Methods – Properties – Constructor – Destructor – Operators – Indexers
Methods syntax [modifiers] return-type method-name([formal-parameter-list]) { statement(s) } Named Arguments string FullName(string firstName, string lastName)… FullName(“Ole”, “Olsen”); FullName(lastName: “Olsen”, firstName: “Ole”); Optional Arguments Method Overloading
Properties syntax [modifiers] type identifier { set {accessor-body} get {accessor-body} } Auto-Implemented public string Age(get; private set;) Choosing Between Properties and Methods
Construction and Disposal Constructors – Instance – Class – Calling Destructor – Why – How (pattern) Dispose vs. Close
Operators Operator Overloading public static Vector operator * (double lhs, Vector rhs) { return new Vector(lhs * rhs.x, lhs * rhs.y, lhs * rhs.z); } public static Vector operator * (Vector lhs, double rhs) { return rhs * lhs; } Console.WriteLine("vect2 * 2 = " + ( vect2 * 2).ToString());
Indexers public char this[int index] { get {... } set {... }
Instance vs. Class Level Static this vs. class name
Struct No inheritance (almost) Constructors Fields are sometimes declared public! Structs are value types
Struct vs. class Footprint Performance
object (System.Object) Methods Comparing for Equality – Reference Types – Value Types MethodAccess string ToString()public virtual int GetHashCode()public virtual bool Equals(object obj)public virtual bool Equals(object objA, object objB)public static bool ReferenceEquals(object objA, object objB)public static Type GetType()public object MemberwiseClone()protected void Finalize()protected virtual
string (System.String) Building Strings – string vs. StringBuilder Formatting – IFormattable Regular Expressions – Matches – Groups – Captures