C# Basics: Muhammad Kamran Rafi
C# Basics: Muhammad Kamran Rafi
Variables Declarations
byte - 0 to 255 char - 2 bytes bool sbyte - -128 to 127 short - 2 byte int ushort - 0 to 65,535 int - 4 bytes uint - 4 bytes positive
Using Variables
C# is Strongly Typed
float x = 10.9f; double y = 15.3; y = x; x = (float) y; // okay // conversion required
Simple Arrays
int[] myArray1 = new int[5];
int[] myArray2 = {1, 2, 3, 4, 5};
Looping Statements
Same as C++
while (count > 0)
process_list (count--);
do
(count)//
Decision Statements
Almost Just Like C++
if (count >= 10) { dostuff(); domorestuff(); } else ... switch (choice) { case 'Y': //must be empty case 'y': do_yes_stuff(); break; default: ...
class modifiers: abstract, static, sealed visibility: public, protected, internal, private interface name starts with I like IComparable, ISerializable const vs. readonly attributes
const, predefined type, compile time evaluation, static readonly, value assigned only once (decl or construct)
Static Classes
They only contain static members. They cannot be instantiated. They are sealed. They cannot contain instance Constructors
Abstract Classes
An abstract class cannot be instantiated. An abstract class may contain abstract methods . It is not possible to modify an abstract class with the sealed modifier, which means that the class cannot be inherited. A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods.
Constructor
public Person (string name, int ssn) { } public Employee(string name, int ssn, int phone) : base(name, ssn) { } public Employee(string name) : this (name, 999, 999) { }
Overriding Methods
Overloading operators
public static bool operator== (MyClass lhs, MyClass rhs) { } public static MyClass operator+(MyClass lhs, MyClass rhs)