csharp

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

CSharp – Basic Structure

CSharp – Basic Structure

CSharp or C# is an Object Oriented language and does not offer global variables or functions. Everything is wrapped in classes, even simple types like int and string, which inherits from the System.Object class.
C# could theoretically be compiled to machine code, but in real life, it’s always used in combination with the .NET framework.

Hello World

1. Open Visual Studio> MAIN TOP MENU> File> New Project> Visual C#> Console Application

2. RIGHT COLUMN> Solutions Explorer> click over ‘yourProgram.cs’

3. Write:

// using keyword imports a namespace, and a namespace is a collection of classes.
using System;
using System.Collections.Generic;
using System.Text;

// tutto lo script è contenuto all'interno del namespace con il nome del nostro file
namespace ConsoleApplication1
{
    // we define our class, every line of code that actually does something, is wrapped inside a class.
    class Program
    {
        // Main is the entry-point of our application,  the first piece of code to be executed
        static void Main(string[] args)
        {
            // Console output
            Console.WriteLine("Hello, world!");
            Console.ReadLine();
        }
    }
}

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

By |CSharp|Commenti disabilitati su CSharp – Basic Structure