C# Basics
Variables, Loops, Decision
Statements, etc
Variables Declarations
byte - 0 to 255 float
char - 2 bytes double
bool decimal
sbyte - -128 to 127 long
short - 2 byte int ulong
ushort - 0 to 65,535
int - 4 bytes string
uint - 4 bytes positive
Using Variables
C# is Strongly Typed
float x = 10.9f;
double y = 15.3;
y = x; // okay
x = (float) y; // conversion required
Variables must have Value before being used.
Hence declarations usually include initialization
Simple Arrays
int[] myArray1 = new int[5];
int[] myArray2 = {1, 2, 3, 4, 5};
for (int i=0;i<10; i++)
Console.Write (myarray[i]);
Console.WriteLine();
Looping Statements
Same as C++
while (count > 0)
process_list (count--);
do
process_list( ++count );
while (count < 10);
for (int i=1; i<=10; i++)
Different from C++
while (count)// illegal C# unless count is bool
Decision Statements
Almost Just Like C++
if (count >= 10) switch (choice)
{ {
dostuff(); case 'Y': //must be empty
domorestuff(); case 'y': do_yes_stuff();
} break;
else default:
... ...
Simple Console Output
System.Console.WriteLine
System.Console.Write
System.Consolue.WriteLine ("count = {0} and sum = {1}",
count, sum);
Simple Console Input
string inputline;
char charvalue;
int intvalue;
Console.Write ("Enter a string: ");
inputline = Console.ReadLine();
Console.WriteLine("You just entered \"{0}\"",inputline);
Console.Write ("Enter a character: ");
charvalue = (char) Console.Read();
Console.WriteLine("You just entered \"{0}\"", charvalue);
Console.ReadLine();
Console.Write ("Enter an integer: ");
inputline = Console.ReadLine();
intvalue = Convert.ToInt32(inputline);
Console.WriteLine("You just entered \"{0}\"", intvalue);
Class/Interface Definitions Like
Java
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.
Inheritance (C++ syntax)
public class Person : IComparable
{
public class Employee : Person, ISerializable
{
}
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
Must mark all overridable methods in the
superclass with ‘virtual’
public virtual int foo()
Must mark all overriding methods in the
subclass with ‘override’
public override int foo()
Use base.MethodName() to call the
superclass method
Overloading operators
Like C++, we can overload and redefine
operators like == and +.
public static bool operator== (MyClass lhs, MyClass rhs) { }
public static MyClass operator+(MyClass lhs, MyClass rhs)