Week 7

Below is some research I did into the basics of C# code to get me back up to speed on with coding.

VARIABLES

<visibility> <data type> <name> = <value>; 

And with an example:

private string name = "John Doe";



IF STATEMENTS

  • 2 conditions on an if statement
OR

number > 10  OR  number < 0 

|| = OR
if((number > 10) || (number < 0))
    Console.WriteLine("Hey! The number should be 0 or more and 10 or less!");
else
    Console.WriteLine("Good job!");
AND

number <= 10  AND  number >=0

&& = AND
if((number <= 10) && (number >= 0))
    Console.WriteLine("Good job!");
else
    Console.WriteLine("Hey! The number should be 0 or more and 10 or less!");

SWITCH STATEMENT

Console.WriteLine("Do you enjoy C# ? (yes/no/maybe)");
string input = Console.ReadLine();
switch(input.ToLower())
{
    case "yes":
    case "maybe":
        Console.WriteLine("Great!");
        break;
    case "no":
        Console.WriteLine("Too bad!");
        break;
}

LOOPS

WHILE LOOPS
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = 0;

            while(number < 5)
            {
                Console.WriteLine(number);
                number = number + 1;
            }

            Console.ReadLine();
        }
    }
}
DO LOOPS
do
{
    Console.WriteLine(number);
    number = number + 1;
} while(number < 5);
  • once the number is more than 5, the loop is exited.
FOR LOOP
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = 5;

            for(int i = 0; i < number; i++)
                Console.WriteLine(i);

            Console.ReadLine();
        }
    }
}
  • You know how many iterations you want
  • int number is only set at the start of the loop, not again
FOREACH LOOP
using System;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {            
            ArrayList list = new ArrayList();
            list.Add("John Doe");
            list.Add("Jane Doe");
            list.Add("Someone Else");
            
            foreach(string name in list)
                Console.WriteLine(name);

            Console.ReadLine();
        }
    }
}
  • For each varibale in array list print them

FUNCTIONS

<visibility> <return type> <name>(<parameters>)
{
	<function code>
}
CALLING A FUNCTION
  • To call a function, you simply write its name, an open parenthesis, then parameters, if any, and then a closing parenthesis, like this:
DoStuff();
public void DoStuff()
{
    Console.WriteLine("I'm doing something...");
}
  • The first part, public, is the visibility, and is optional. If you don’t define any, then the function will be private.
  • Void is the return type – means it returns nothing
public int AddNumbers(int number1, int number2)
{
    int result = number1 + number2;
    return result;
}
  • int = return type, so the function returns an interger
  • public = the function is publically visible
public int AddNumbers(int number1, int number2)
{
    int result = number1 + number2;
    if(result > 10)
    {
        return result;
    }
    return 0;
}
  • As long as the resutl is above 10 the result will be printed and if not then 0 will be.

FUNCTION PARAMETERS

  • Information is passed by reference not actual value – Not changing the value of the
THE REF MODIFIER
  • Use to modify the the value of a variable within a function
static void Main(string[] args)
{
    int number = 20;
    AddFive(ref number);
    Console.WriteLine(number);
    Console.ReadKey();
}

static void AddFive(ref int number)
{
    number = number + 5;
}
THE OUT MODIFIER
  • confusing stuff!!
PARMS MODIFIER
  • Function for a arbitary number of functions
static void GreetPersons(params string[] names) { }

Calling it would then look like this:

GreetPersons("John", "Jane", "Tarzan");