CSharp – Switch Statement

CSharp – Switch Statement

The switch statement is like a set of if statements. It’s a list of possibilities, with an action for each possibility, and an optional default action, in case nothing else evaluates to true.

switch case


using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            int caseSwitch = 1;
            switch (caseSwitch)
            {
                case 1:
                    Console.WriteLine("Case 1");
                    break;
                case 2:
                    Console.WriteLine("Case 2");
                    break;
                // if case 1 and case 2 are false:
                default:
                    Console.WriteLine("Default case");
                    break;
            }
        } // End Main()
    }
}

switch case – while

How to create a loop:

...
case 4:
    while (true)
        Console.WriteLine("Endless looping. . . .");
...
By |CSharp, Video Games Development|Commenti disabilitati su CSharp – Switch Statement

CSharp – if Statement

CSharp – if Statement

With if statement you can set up conditional blocks of code.

if – else if – else


using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter a character: ");
            char ch = (char)Console.Read();

            if (Char.IsUpper(ch))
            {
                Console.WriteLine("The character is an uppercase letter.");
            }
            else if (Char.IsLower(ch))
            {
                Console.WriteLine("The character is a lowercase letter.");
            }
            else if (Char.IsDigit(ch))
            {
                Console.WriteLine("The character is a number.");
            }
            else
            {
                Console.WriteLine("The character is not alphanumeric.");
            }
        } // End Main()
    }
}

if – nested


using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter a character: ");
            char c = (char)Console.Read();
            if (Char.IsLetter(c))
            {
                if (Char.IsLower(c))
                {
                    Console.WriteLine("The character is lowercase.");
                }
                else
                {
                    Console.WriteLine("The character is uppercase.");
                }
            }
            else
            {
                Console.WriteLine("The character isn't an alphabetic character.");
            }
        } // End Main()
    }
}

if – boolean


using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            // Change the values of these variables to test the results.
            bool Condition1 = true;
            bool Condition2 = true;
            bool Condition3 = true;
            bool Condition4 = true;

            if (Condition1)
            {
                // Condition1 is true.
            }
            else if (Condition2)
            {
                // Condition1 is false and Condition2 is true.
            }
            else if (Condition3)
            {
                if (Condition4)
                {
                    // Condition1 and Condition2 are false. Condition3 and Condition4 are true.
                }
                else
                {
                    // Condition1, Condition2, and Condition4 are false. Condition3 is true.
                }
            }
            else
            {
                // Condition1, Condition2, and Condition3 are false.
            }
        } // End Main()
    }
}

if – AND NOT


using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            // NOT
            bool result = true;
            if (!result)
            {
                Console.WriteLine("The condition is true (result is false).");
            }
            else
            {
                Console.WriteLine("The condition is false (result is true).");
            }

            // Short-circuit AND
            int m = 9;
            int n = 7;
            int p = 5;
            if (m >= n && m >= p)
            {
                Console.WriteLine("Nothing is larger than m.");
            }

            // AND and NOT
            if (m >= n && !(p > m))
            {
                Console.WriteLine("Nothing is larger than m.");
            }

            // Short-circuit OR
            if (m > n || m > p)
            {
                Console.WriteLine("m isn't the smallest.");
            }

            // NOT and OR
            m = 4;
            if (!(m >= n || m >= p))
            {
                Console.WriteLine("Now m is the smallest.");
            }
            // Output:
            // The condition is false (result is true).
            // Nothing is larger than m.
            // Nothing is larger than m.
            // m isn't the smallest.
            // Now m is the smallest.
        } // End Main()
    }
}

My Website: http://www.lucedigitale.com

References:
http://msdn.microsoft.com/it-it/library/5011f09h.aspx
http://csharp.net-tutorials.com/basics/if-statement/

By |CSharp, Video Games Development|Commenti disabilitati su CSharp – if Statement

CSharp – ArrayList

CSharp – ArrayList

To use ArrayList you need: using System.Collections;


using System;
using System.Collections; // class for ArrayList

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // <type> <name of the list> = new ArrayList()
            ArrayList list = new ArrayList();
            // add an element
            list.Add("Mario");
            list.Add("Luca");
            list.Add("Giovanni");

            // per ogni elemento nella lista
            foreach (string name in list)
                Console.WriteLine(name);

            Console.ReadLine();
        }
    }
}

The result is:
Mario
Luca
Giovanni

Complete Reference: http://msdn.microsoft.com/it-it/library/system.collections.arraylist(v=vs.110).aspx

By |CSharp, Video Games Development|Commenti disabilitati su CSharp – ArrayList

CSharp – Array

CSharp – Array

Array – foreach


using System;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // <un array di stringhe> <nome array> = new string[numero elementi nell'array]
            string[] names = new string[2];

            // array[indice] = assegna il valore
            names[0] = "Andrea";
            names[1] = "Mario";

            // visualizza tutti i valori contenuti
            foreach (string s in names)
                Console.WriteLine(s);

            Console.ReadLine();
        }
    }
}

The result is:
Andrea
Mario

Array – for

...
for(int i = 0; i < names.Length; i++)
    Console.WriteLine("Item number " + i + ": " + names[i]);
...

Array – short syntax

...
int[] numbers = new int[5] { 4, 3, 8, 0, 5 };
...
int[] numbers = { 4, 3, 8, 0, 5 };
...

My website: http://www.lucedigitale.com

Reference: http://csharp.net-tutorials.com/basics/arrays/

By |CSharp, Video Games Development|Commenti disabilitati su CSharp – Array

CSharp – Variables and Data Types

CSharp – Variables and Data Types

Data types

You are required to inform the compiler about which data types you wish to use every time you declare a variable.

byte —> 0 .. 255

sbyte —> -128 .. 127

short —> -32,768 .. 32,767

ushort —> 0 .. 65,535

int —> -2,147,483,648 .. 2,147,483,647

uint —> 0 .. 4,294,967,295

long —> -9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807

ulong —> 0 .. 18,446,744,073,709,551,615

float —> -3.402823e38 .. 3.402823e38

double —> -1.79769313486232e308 .. 1.79769313486232e308

decimal —> -79228162514264337593543950335 .. 79228162514264337593543950335

char —> a Unicode character.

string —> a string of Unicode characters.

bool —> true or false.

object —> an object.

Example with strings:


// Working with strings

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // <datatype> <name>
            string firstName = "John";
            string lastName = "Doe";

            Console.WriteLine("Name: " + firstName + " " + lastName);

            Console.WriteLine("Please enter a new first name:");
            // get new value from console 
            firstName = Console.ReadLine();

            Console.WriteLine("New name: " + firstName + " " + lastName);

            Console.ReadLine();
        }
    }
}

Example with math:


// Working with math

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            // integer numbers
            int number1, number2;

            Console.WriteLine("Please enter a number:");
            // It reads a string and converts it into an integer. 
            number1 = int.Parse(Console.ReadLine());

            Console.WriteLine("Thank you. One more:");
            number2 = int.Parse(Console.ReadLine());

            Console.WriteLine("Adding the two numbers: " + (number1 + number2));

            Console.ReadLine();
        }
    }
}

Constants

The constants refer to fixed values that the program may not alter during its execution.

The syntax is:

const <data_type> <constant_name> = value;

Example:


using System;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            const double pi = 3.14159; // constant declaration 
            double r;
            Console.WriteLine("Enter Radius: ");
            r = Convert.ToDouble(Console.ReadLine());
            double areaCircle = pi * r * r;
            Console.WriteLine("Radius: {0}, Area: {1}", r, areaCircle);
            Console.ReadLine();
        }
    }
}

By |CSharp, Video Games Development|Commenti disabilitati su CSharp – Variables and Data Types