c# type
bool: true/false.
char: default=""x0000"".
sbyte: signed byte.
short: (2byte) integer from -32k to 32k.
- int: (4byte) integer from -2B to 2B
- int.Parse(string) : Converts from string to # int.ToString(""x""): convert to hex string int.ToString(""d""): decimal format int.ToString(""f""): fixed-point int.ToString(""r""): roundtrip format
- long: (8byte)L
- to print hex, use ToString(""X"")
byte: unsigned
ushort
uint: U
ulong: UL
float: 0.0f
double: 0.0d
decimal: 0.0m
Type tests
GetType() returns type reference of any var/type
GetType().IsAbstract:
- IsArray:
- myArr.GetType().IsArray : returns true
IsClass
IsEnum
IsInterface
IsPointer
IsPrimitive (one of the predefined primitive data types),
IsPublic
IsSealed
IsValueType: true if struct/enum (not verified)
typeof(obj)
Type t = typeof(MyClass); if (typeof(obj)==Type.GetType(DateTime)) // it is DateTime
- is : check if object is compatible with given type (ie can be cast)
- if (obj is Class1) { a=(Class1)obj; }...
- Expression (ternary operator)
- x ? trueExpr : falseExpr ex: return x?3:77;
Constants
const float pi = 3.1415927f;
readonly var=x;
public static readonly myVar=7;
- Similar to const but it can be changed in constructor.
- public readonly myTime=DateTime.Now.Ticks;
Enum
default: uses int
enum Color {red,green,blue=5, purple= blue+24 }
Color a = Color.red;
- Another Type: not using int:
- enum Color : sbyte { ... }
Casting:
int i = (int)Color.red; Color c = (Color)1; // green
enum value as string outputs the string value, not int value. enum2str is necessary!:
WriteLine(Color.red); // output: ""red""
WriteLine(Color.red.ToString(""d"")); // output: ""0"" in string
- .Parse(typeof(<enumType>),""string"")
- parses the string and converts to appropriate enum value. Color currentColor =(Color)Enum.Parse(typeof(Color), ""blue"");
- .GetValues(typeof(<enumType>)) : returns array of enums
- Array arr = Enum.GetValues(typeof(Color)); // {""red"",""green"",""blue""}
[Flags]: attribute that sets this as a bit-combined flag
[Flags]
enum FONTF : byte { BOLD=1,ITALIC=2,UNDERLINE=4, SUPER=8, SUB=16}
FONTF myFont = FONTF.BOLD | FONTF.SUPER;
WriteLine (myFont); // output: ""BOLD, SUPER""
WriteLine (myFont.ToString(""d"")) ; // output: ""9""
Array
int[] a = new int[10];
Once array's size has been set, it cannot be changed.
- initializing::
- int[] a = new int[] {1, 2, 3}; // long version int[] a = {1, 2, 3}; // short-version
Dynamically creating array:
int arraySize=10; string[] arr =new string[arraySize];
Multidimension:
int[,] a2 = new int[10, 5]; // multidim array int[,,] a3 = new int[10, 5, 2];
jagged array:
int[][] a = new int[3][]; a[0] = new int[10]; a[1] = new int[5]; a[2] = new int[20];
.Length : get size of array
Array.Sort(myArray); // sorts ""myArray""
- Array.CopyTo(destArray, destArrayIndexToInsert)
- copies all elements in this array to the destArray starting at dest's index n.
Struct
Struct is similar to class and can have methods as well. However, struct exists in stack and class exists in heap. There is no need for new() in struct because it can exist in stack. However, class requires new().
