Show Menu
Cheatography

Introduction to programming - Final Term Cheat Sheet (DRAFT) by

Introduction to programming - Final Term

This is a draft cheat sheet. It is a work in progress and is not finished yet.

While

while (!isValid)
{
    Console.WriteLine("Enter score{0}: ", i + 1);
    inValue = Console.ReadLine();
    isValid = int.TryParse(inValue, out score[i]);

    if (!isValid)
    {
        Console.WriteLine("Invalid data entered -" + "0 stored in array");
    }
}

Arrays

Array Declare
int[] scores = new int[14];
Array Initialize
int[] anArray = { 100, 200, 400, 600 };
Copy
Copy (Syste­m.Array source­Array, int index1­Source, System.Array target­Array, int index1­Target, int length­ToCopy)
Read User Input
inValue = Consol­e.R­ead­Line();

Advanced Collec­tions

Two dimens­ional Arrays
int[,] calories = { { 900, 750, 1020 }, { 300, 100, 2700 }, { 200,30­0,5500 } };
Two dimens­ional Arrays Init
int[,] calories = new int[7,3];
ArrayList
ArrayList anArray = new ArrayList( ); // Instan­tiates ArrayList
List
List<I­tem> items = new List<I­tem­>()

Two dimens­ional Arrays Example

public static double[] CalulateAverageByDay(int[,] calories) {
    int sum = 0;
    double[] dailyAverage = new double[7];
    // 7 
    for (int r = 0; r < calories.GetLength(0); r++) {
        // 3 
        for (int c = 0; c < calories.GetLength(1); c++) {
            sum += calories[r, c];
        }
        dailyAverage[r] = (double) sum / calories.GetLength(1);
        sum = 0;
    }
    return dailyAverage;
}

Progra­mming Based on Events

Delegates
delegate string Return­sSi­mpl­eSt­ring( );
Event Handler Methods
this.b­utt­on1.Click += new System.Ev­ent­Han­dle­r(t­his.bu­tto­n1_­Click);

Object Oriented Language Features

Abstra­ction
Genera­lizing, identi­fying essential features, hiding noness­ential comple­xities
Encaps­ulation
Packaging data and behaviors into a single unit, hiding implem­ent­ation details
Inheri­tance
Extending program units to enable reuse of code
Polymo­rphism
Providing multiple (diffe­rent) implem­ent­ations of same named behaviors

Component based applic­ations

Business layer
Classes that perform processing / Provide functi­onality for specific system; provide necessary processing
Data access layer
Classes for accessing data from text files and database
Presen­tation layer
User interface tier for user intera­ction / Graphical User Interface such as Windows or Web
 

Inheri­tance

Associated with an “is a” relati­onship
Classes can also have a “has a” relati­onship, not associated with inheri­tance
“has a” relati­onship is associated with contai­nment or aggreg­ation (집합)
Every object inherits four methods (Equals / GetHas­hCode / GetType / ToString)

Overriding Methods

// In order to be an method that can be overridden, keyword “virtual”, “abstract”, or “override” must be part of the heading for the parent class 
public override string ToString() {
	return firstName + " " + lastName;
}

Creating Derived classes

public class Student: Person {
	private string major;
	private string studentId;

	public Student(string id, string lName, string fName, int age, string major, string sId )
	: base(id, lName, fName, age) {
	}
}

Abstract

Classes
public abstract class Example {}
Methods
public abstract string Method();

Generics

// Reduce the need to rewrite algorithms for each data type 
public class GenericClass<T>
{
	public T dataMember;
}
GenericClass <string> anIdentifier = new GenericClass<string>();

Working with Files

Import
using System.IO;
Exist
File.E­xis­ts(­fil­eName)
Enumer­ation
public enum DayOfWeek { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday } Consol­e.W­rit­eLi­ne(­"­Today is {0}!", DayOfW­eek.Tu­esday);
Direct­oryInfo
Direct­oryInfo dir = new Direct­ory­Inf­o("."­);
Stream­Writer
Stream­Writer outputFile = new Stream­Wri­ter­("sa­yin­g.t­xt"); output­Fil­e.W­rit­eLi­ne(­"This is the first line in a text file");
Stream­Reader
Stream­Reader inputFile = new Stream­Rea­der­("so­meI­npu­tFi­leN­ame­"); string inValue = inputF­ile.Re­adL­ine();

Inheri­tance - Access Modifiers

public
Accessible everywhere
private
Accessible only within the class
protected
Accessible within the class and derived classes