C# Fundamentals

What is C#?

.Net Framework

Provides an environment to build and run .net framework applications

Common Language Runtime(CLR)?

The unit that executes .net framework applications

 MS Intermediate Language(IL/MSIL)?

Portable Assembly for JIT

Just-In-Time (JIT) Compiler?

Converts IL/MSIL to assembly.

So applications can run on any .Net framework  environment.

Namespaces

using System;
using System.Collections.Generic;
namespace MyProject
{
    public class MyClass
    {
      ...
    }
}

Variables

<type> <name>;

 

int age;

Constants

const int x = 0;

const string productName = "Visual C#";

 

const double x = 1.0, y = 2.0, z = 3.0;

Statements 

// Declaration statement. 
int counter;

// Assignment statement.
counter = 1;

// foreach statement block that contains multiple statements. 
foreach (int radius in radii){}

Expressions

Console.WriteLine("Hello World")

 

y = x+5

 

myString = "Hi!";

 

if ( age == 15)

Operators

  • + operator ... both math and string concatenation
  • = operator ... variable assignment
  • == operator ... test for equality
  • () operator ... method invocation - Console.WriteLine()
  • . operator ... member access
  • > operator
  • < operator
  • etc...

Data Types

Type

Alias For

Allowed Values

byte

System.Byte

Integer between 0 and 255

int

System.Int32

Integer between −2147483648 and 2147483647

bool

System.Boolean

Boolean value, true or false

string

System.String

A sequence of characters

 

etc...

enumarations

enum Days { Sun, Mon, Tue, Wed, Thu, Fri, Sat }; //Starts from 0

  //OR

enum Days { Sun = 1, Mon, Tue, Wed, Thu, Fri, Sat }; //Starts from 1


static void Main() 
{
     int x = (int)Days.Sun;
     int y = (int)Days.Fri;
     Console.WriteLine("Sun = {0}", x);
     Console.WriteLine("Fri = {0}", y);
}

Arrays

type[] arrayName;


// Declare a single-dimensional array  
int[] array1 = new int[5];

// Declare and set array element values 
int[] array2 = new int[] { 1, 3, 5, 7, 9 };

// Alternative syntax 
int[] array3 = { 1, 2, 3, 4, 5, 6 };

// Declare a two dimensional array 
int[,] multiDimensionalArray1 = new int[2, 3];

// Declare and set array element values 
int[,] multiDimensionalArray2 = { { 1, 2, 3 }, { 4, 5, 6 } };

Collections - List<T>

// Create a list of strings. 
var salmons = new List<string>();

salmons.Add("chinook");
salmons.Add("coho");
salmons.Add("pink");
salmons.Add("sockeye");

// Create a list of strings by using a 
// collection initializer. 
var salmons = new List<string> { "chinook", "coho", "pink", "sockeye" };

Program Flow Control Mechanisms

  • The if statement
  • The switch statement
  • Looping

"if" statement

if (<test>)
{
   <code executed if <test> is true>;
}
else
{
   <code executed if <test> is false>;
}

"switch" statement

switch (<testVar>)
{
   case <comparisonVal1>:
      <code to execute if <testVar> == <comparisonVal1> >
      break;
   case <comparisonVal2>:
      <code to execute if <testVar> == <comparisonVal2> >
      break;
   …
   default:
      <code to execute if <testVar> != comparisonVals>
      break;
}

"while" & "do while" loops

int i = 1;
while (i <= 10)
{
   Console.WriteLine(″{0}″, i++);
}




int i = 1;
do
{
   Console.WriteLine(″{0}″, i++);
} while (i <= 10);

"for" & "for each" loops

int i;
for (i = 1; i <= 10; ++i)
{
   Console.WriteLine(″{0}″, i);
}


var salmons = new List<string> { "chinook", "coho", "pink", "sockeye" };
foreach (var salmon in salmons)
{
    Console.Write(salmon + " ");
}
  • break—Causes the loop to end immediately

  • continue—Causes the current loop cycle to end immediately (execution continues with the next loop cycle)

  • goto—Allows jumping out of a loop to a labeled position (not recommended if you want your code to be easy to read and understand)

  • return—Jumps out of the loop and its containing function 

Interrupting Loops

Classes & Objects

namespace ProgrammingGuide
    {
        // Class definition. 
        public class MyCustomClass
        {
            // Class members: 
            // Property. 
            public int Number { get; set; }

            // Method. 
            public int Multiply(int num)
            {
                return num * Number;
            }

            // Instance Constructor. 
            public MyCustomClass()
            {
                Number = 0;
            }
        }
    }

Properties

public string FullName { get; set; }
public int Age { get; set; }
public string Country { get; set; }

Fields

    // private field 
    private DateTime date;

    // public field (Generally not recommended.) 
    public string day;

Constructors & Initialization

public class Taxi
{
    public bool isInitialized;
    public Taxi()
    {
        isInitialized = true;
    }
}

class TestTaxi
{
    static void Main()
    {
        Taxi t = new Taxi();
        Console.WriteLine(t.isInitialized);
    }
}
public class Employee
{
    public int salary;

    public Employee(int annualSalary)
    {
        salary = annualSalary;
    }

    public Employee(int weeklySalary, int numberOfWeeks)
    {
        salary = weeklySalary * numberOfWeeks;
    }
}

//Usage
Employee e1 = new Employee(30000);
Employee e2 = new Employee(500, 52);

Access Modifiers

  • public - The type or member can be accessed by any other code in the same assembly or another assembly that references it.
  • private  The type or member can be accessed only by code in the same class or struct.
  • protected The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class.
  • internal The type or member can be accessed by any code in the same assembly, but not from another assembly.
  • protected internal​The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.
// public class: 
public class Tricycle
{
    // protected method: 
    protected void Pedal() { }

    // private field: 
    private int wheels = 3;

    // protected internal property: 
    protected internal int Wheels
    {
        get { return wheels; }
    }
}

Methods

public void Caller()
{
    int numA = 4;
    // Call with an int variable. 
    int productA = Square(numA);

    int numB = 32;
    // Call with another int variable. 
    int productB = Square(numB);

    // Call with an integer literal. 
    int productC = Square(12);

    // Call with an expression that evaulates to int.
    productC = Square(productA * 3);
}

int Square(int i)
{
    // Store input argument in a local variable. 
    int input = i;
    return input * input;
}

Resources

thanks...

C# Fundamentals

By Barış SÖNMEZ

C# Fundamentals

An introduction to C# Programming Language

  • 272