C#: Using for loop to find array total

This entry is part 36 of 71 in the series C#

In this example, we demonstrate on using a for loop to find the total sum of an array.

We have talked about property.  Here, we used an array property Length to find the number of element in the array.  This is shown in line 13 where we access the length of the array using array.Length.

This number is needed for the for loop to calculate the total.

```using System;

namespace MyArray
{
class Program
{
static void Main()
{

double[] array = { 2.5, 3.2, 5.21, 6.0 };

double sum = 0;
int length = array.Length;  //finding the length using array propertye Length

for (int i = 0; i < length; i++ )
{
sum = sum + array[i];
}

double average = sum / length;

Console.WriteLine("Sum: {0}", sum);
Console.WriteLine("Average: {0}", average);

}
}
}```

The program begins by initializing a variable sum to 0. Then it loops through the values stored in array, adding each one to sum.

By the end of the loop, sum has accumulated the sum of all values in the array.

The resulting sum is divided by the number of elements to calculate the average.

C#: Using foreach to print out array elements

This entry is part 37 of 71 in the series C#

For array, there is an easy method to print out the array elements.

C# also provides the foreach statement. It provides a simple way to print out the elements of an array.

For this example, the foreach statement will run through the str array, put the elements into iStr and print out each element it encounters.

```using System;

namespace ArrayDisplay
{
class Program
{
static void Main()
{
string[] str = { "The", "End", "Of", "The", "Innocence" };

foreach (string iStr in str)
{
Console.Write("{0} ", iStr);
}
}
}
}```

The output from running the above code will be The End Of The Innocence.

C#: Converting foreach program into OOP style

This entry is part 38 of 71 in the series C#

When it comes to learning OOP style of programming, it is good to convert even a simple program into object-oriented style.

From our previous example, we convert it into using displayArray method to output the array string.

```using System;

namespace ArrayDisplay
{
class Program
{
private string[] str = { "The", "End", "Of", "The", "Innocence" };

private void displayArray()
{
foreach (string iStr in str)
{
Console.Write("{0} ", iStr);
}
}

static void Main()
{
Program display = new Program();
display.displayArray();
}
}
}```

Posted in

C#: Passing parameters into a constructor

This entry is part 39 of 71 in the series C#

When it comes to constructing a constructor, take note that its name is always similar to the class name.

In our example below, the class name is Program and so the constructor name is also Program.

```using System;

namespace ArrayDisplay
{
class Program
{
private string[] str;

private Program(string[] str1)
{
str = str1;
}

private void displayArray()
{
foreach (string iStr in str)
{
Console.Write("{0} ", iStr);
}
}

static void Main()
{
string[] str1 = { "The", "End", "Of", "The", "Innocence" };

Program display = new Program(str1);
display.displayArray();
}
}
}```

Posted in

C#: If Else

This entry is part 40 of 71 in the series C#

The if else statement is a conditional branch statement.

If you are new to programming, this will be one of the first things that you should learn.

We can illustrate it by using the below statements:

```if (expression)
{
statement 1;
}
else
{
statement 2;
}```

The expression is actually a Boolean expression.  It should resolve to either true or false.

If the expression resolves to true, statement 1 will be run.  If the expression resolves to false, statement 2 will be run.

Since the expression is a Boolean expression, let’s use the example below where it will check if a Boolean variable is set to true or false.

```using System;

namespace Conditional
{
class Program
{
static void Main()
{
bool test = false;

if (test)
{
Console.WriteLine("The test boolean variable is set to true.");
}
else
{
Console.WriteLine("The test boolean variable is set to false.");
}
}
}
}```

In line 11, we have if (test).

This is actually similar to if (test == true) as shown in the example below, with the minor modification.

If Else
```using System;

namespace Conditional
{
class Program
{
static void Main()
{
bool test = false;

if (test == true)
{
Console.WriteLine("The test boolean variable is set to true.");
}
else
{
Console.WriteLine("The test boolean variable is set to false.");
}