Arrays

Arrays are ways of having a block of memory allocated to a single variable type, this allows for holding the national lottery numbers within an array of 6 instead of actually having 6 different variables, saves on variable overload

int value1 = new int (1);
int value2 = new int (2);
int value3 = new int (3);
int value4 = new int (4);
int value5 = new int (5);
int value6 = new int (6);

and instead you could just have

int[] values = new int[6] {1,2,3,4,5,6};

This is example code of how to use arrays.

using System;
 
class arrays
{
       public static void Main()
       {
              // the [] means a array declaration, and when you create the new instance of a int you then define the size (3 in this case)
              // if you wish to define the default values, then place these within the {} brackes
              int[] arrayInt = new int[3] { 3,2,1};       
 
              // for each (integer in the array of integers assign to i)
              foreach (int i in arrayInt)
                     Console.WriteLine(i);
 
              // to alter the values within the array, an array always starts at 0
              arrayInt[0] = 20;
              arrayInt[1] = 30;
              arrayInt[2] = 40;
 
              foreach (int i in arrayInt)
                     Console.WriteLine(i);
              return;
       }
}

Save as array.cs and then compile using either mono/csc.exe. The output will be

3
2
1
20
30
40

Method – Add Two numbers

This tutorial uses the same code as Add two numbers but includes a function/method to add two numbers together and return a value. In this case the value is a integer (int) and that is why the int is just before the name of the method (addIntegers) with two parameters passed to the method, these are the values that are being added together. I have called them a and b, so that they are different names to the variables within the main method.

The source code

using System;       // for the console object
 
class addtwonumbers
{
       public static int addIntegers(int a, int b)
       {
              return (a + b);
       }
 
       public static void Main()
       {
              try 
              {
                     // output to the console
                     Console.Write("Please enter value 1 : ");
                     // readin the console input, and then convert to a integer value
                     int val1 = Convert.ToInt32(Console.ReadLine());
                     Console.Write("Please enter value 2 : ");
                     int val2 = Convert.ToInt32(Console.ReadLine());
                     // write out the answer 
                     Console.WriteLine("Answer = " + addIntegers(val1,val2));
              }
              catch (Exception e)
              {
                     // any errors. Like converting a string to a integer value
                     Console.WriteLine("Error : " + e.ToString());
              }
       }
}

save as addtwonumbers_function.cs, this program will function the same as the previous tutorial apart from the inner working will call the method. The method programming allows for one method to be called multiple times, e.g. Within the example above there could be a method that was 5 lines in length and instead of writing the same 5 lines each time, you just write a method that is called.

Add two numbers

This tutorial will demonstrate how to read from the input console (console line) to answer the prompts. The prompts will need to be a integer value to add the two inputs together.

I am using the System.Console object to read from the console and then converting the string inputs into integers with the Convert.ToInt32 function (the System.Convert has many methods and ToInt32 is one of these).

The code

using System;       // for the console object
 
class addtwonumbers
{
       public static void Main()
       {
              try 
              {
                     // output to the console
                     Console.Write("Please enter value 1 : ");
                     // readin the console input, and then convert to a integer value
                     int val1 = Convert.ToInt32(Console.ReadLine());
                     Console.Write("Please enter value 2 : ");
                     int val2 = Convert.ToInt32(Console.ReadLine());
                     // write out the answer 
                     Console.WriteLine("Answer = " + (val1 + val2));
              }
              catch (Exception e)
              {
                     // any errors. Like converting a string to a integer value
                     Console.WriteLine("Error : " + e.ToString());
              }
       }
}

save that as addtwonumbers.cs.

Once compiled (mcp addtwonumbers.cs with mono) and executed (mono addtwonumbers.exe) the output will be

Please enter value 1 : 30
Please enter value 2: 23
Answer = 53

Class structure

This is a general tutorial about the class structure of programming languages.

The class is basically an object that allows public methods (accessed outside that class object) and private methods (the internal class logic) and protected with is the same as private apart from an inherited class can access the protected objects. Basically the 3 main types

Public : Access from outside the class, and internal of course, with inherited/friends etc.
Protected : Access from inherited/friends of the class, but not called from the class object.
Private : Access from only the class and friends of the class.

For example within C# coding …

class example1
{
       private int value1;
       private int value2;
       protected int value3;
 
       public int addtwo(int a, int b)
       {
              value1 = a;
              value2 = b;
              value3 = (a+b);              // setup the local variables
              return value3;
       }
 
       public int returnValue()
       {
              return value1;
       }
}
 
class example2 : example1
{
       public int returnValue2()
       {
              return value2;       // will error due to value2 is private in the inherited class
              //return 0;              // to be able to compile
       }
 
       public int returnValue3()
       {
              return value3;       // fine since a protected part of the inherited class
       }
}
 
class examples 
{
       static public void Main()
       {
              example1 ex = new example1();
              example2 ex2 = new example2();
              // add the two numbers and set the local internal variables
              System.Console.WriteLine("Two numbers 3 + 4 = " + ex.addtwo(3,4));
              // output the value1 from the internal example1 class
              System.Console.WriteLine("Value1 = " + ex.returnValue());
 
              // Just for demo'ing, since the example2 is not linked to example1, just inherites it.
              System.Console.WriteLine(ex2.returnValue2());       // will not compile the class above.
              System.Console.WriteLine(ex2.returnValue3());
              return;
       }
}

save as classexample.cs. The output will be

Two numbers 3 + 4 = 7
Value1 = 3
0
0

The two zero’s are because class example2 only inherits the example class and not does not link to it. I have added the line within the example2 returnValue2 method to be able to compile the source code otherwise you will get the error similar to

classexample.cs(25,10): error CS0122: example1.value2 is inaccessible due to its protection level

Local variables

This is a tutorial about variable scope, a variable is a means to store data within the context of the program. For example, if you wish to store a numerical value then you would not store that number in a string, but within a integer or floating point number.

There are different types of variables, integer / string etc, but the scope of a variable is the area in which the variable is defined within block of code that it is situated.

This is some c# code to demonstrate the difference between local variables and global.

class localvariable
{
       private static int value1 = 1;
 
       private static void global()
       {
              System.Console.WriteLine("Global : " + value1);
       }
 
       public static void Main()
       {
              int value1 = 0;
              System.Console.WriteLine("Local : "  + value1);
              global();
              return;
       }
}

save this as localvariable.cs and then compile up the code and run. The output should be.

Local : 0
Global : 1

and as you can tell there are two value1 within the source code whilst one is the global variable and the other is local to the Main method.

Read and Write to files

This tutorial is about how to read and write from/to a file (textual file).

First of all define the files that are for the input and output filenames.

NOTE: the { } are the begin and end of a code structure, e.g. While begin; do something; end;

string fileName = "country.txt";
string outFileName = "sqlcountry.txt";
 
//make sure that the file is present
if (File.Exists(fileName))
{
 
//Setup the output file object, using a StreamWriter to write to files.
       using (StreamWriter SWriter = new StreamWriter(outFileName))
       {
 
//Setup the input file object, 
       using (StreamReader SReader = new StreamReader(fileName))
       {
 
//The line is the input line from the input file.
       String line;
 
//While assigning the line from the input file is valid
       while ((line = SReader.ReadLine()) != null){
 
//Write to the output file some different text.
       SWriter.WriteLine("insert into country(place) values(\"" + line + "\");");
}

I have attached all of the required files to be able to test this tutorial.

Hello world

This is a classic Hello World, if you are using Windows then there should be included with it .net, which has the .net runtime environment (this is stored in C:\WINDOWS\Microsoft.NET\Framework\v*). If you are using another OS or wish to try out Mono (Try Mono), which is a open source .net compiler and run-time environment.

The language is similar to c++ and also Java. The main aspect is that it envolves from class and because of that System is a class which in-turn has the Console sub class and then the method WriteLine.

The helloworld code example

public class helloworld
{
       public static void Main(string[] args)
       {
              System.Console.WriteLine("Hello world!");
       }
}

save as helloworld.cs

To compile with MS .net

csc helloworld.cs

which creates a executable file to execute.
helloworld.exe
with the output

Hello world!

or within mono

mcs helloworld.cs

and then to run the program you can either just type in helloworld.exe as like MS .net or use the JIT (Just In Time) environment, mint.

mint helloworld.exe