Monday, 12 June 2017

Compare C# and Scala Programming Languages - Part 2

This is the second part of our comparison between Scala and C# Programming Languages

In this part we are going to examine:
  • Arrays Declaration and Manipulation
  • Collections(List and Sets)
  • Pointers
Then we would look at Object Oriented Programming Concepts:

  • Polymorphism
  • Inheritance
  • Encapsulation

Arrays


An array holds a fixed-size sequential collection of items which are of the same data type. An array is generally used to store a collection of data or variables of the same time in contiguous memory location.
An example is when we have 20 different scores from students in a class. We made declare and array variable called Scores[19] that holds all the 20 scores. This would be easier than declaring 20 different variable say Score1, Score2, Score3 etc.

Arrays Declaration and Manipulation in C#
To declare an array in C#, you can use the following syntax:

datatype[] arrayName;

datatype is used to specify the data type of the elements in the array. Remember that all elements of an array would be of the same data type.

[ ] is used to specify the size of the array

arrayName specifies the name of the array

Using the student scores example, we have declare an array as follows:

int[ ] Scores;

Initializing an Array in C#
Array declaration does not initialize the array in the memory. For you to assign values to and array variable, the array variable is initialized. An array is a reference type, and this means that the new keyword shod be used to create and instance of an array. For example:

double[ ] Scores = new double[10]

Assigning Values to an Array
Values can be assigned to individual elements of an array.. This is achieved by using the index number (also called subscript) as shown below:

double[ ] Scores = new double[10];
Score[0] = 80.5;
Score[1] = 65.0;

You can also assign values to the array at the time it is declares as shown below:

double[ ] Scores = {80.5, 65.0, 70, 93.6}

The code above declares an array called Scores and assigns 4 elements to the array.

When an array is created in C#, the compiler implicitly initializes the array elements to a default value depending on the type of the array. An example would be an array declared as int, all the elements are initialized to 0;

Accessing Array Elements in C#
An array element is accessed by using an index with the array nam.e this is done by placing the index of the element to be accessed within the square brackets after the name of the array. An example is given below:

double MaxScore = Score[5];

This code retrieves that value of the array element Score[5] and stores it into a variable name MaxScore.


Array Declaration and Manipulation in Scala
Arrays in Scala share the similar meaning and behavior with C# so lets consider Array Declaration, Array Initialization and Access in Scala.

Declaring an Array Variable in Scala
The syntax for declaring an array variable in Scala is:

var x:Array[String] = new Array[String](10)

or

var x = new Array[String](10)

In the above example, x is declared as an array of Strings that may hold up to 10 elements. Values can be assigned to the individual elements of the array. Additionally, values of the elements can be retrieved like:

x(1) = "Kindson";
x(2) = "Oleander";
x(3) = "Othniel";

and so on.
You can also assign values as follows

var x = Array("Osondu", "Jackasi", "Adaku", "Othniel", "Kindson")


Collections


Collections are classes that as specialized to handle data storage and retrieval. These classes provide support for data structures such as stacks, queues, trees, lists etc.
Collections could be used for operations such as dynamic memory allocation, accessing list of items using the index, creating a list by assigning values to the elements etc.

Collections in C#
Four of the collections classes in C# are outlined below.

ArrayList
This represents an ordered collection of objects that can be referenced individually. The ArrayList is similar to the Array, but the ArrayList has more features. For instance, you can add or remove items from a specific location in an ArrayList, and the ArrayList would automatically adjust to reflect the changes.
An ArrayList supports dynamic memory allocation.

SortedList
In addition to index, the SortedList also uses a key to access items in the list. A SortedList combines the features of both an ArrayList and a Hashtable. It contains a list of elements that can be access by using a key or an index. When element is accessed using a key, then it is a Hashtable, if accessed using an index, then it becomes an array. The unique feature of the SortedList is that it is always sorted based on a key value.

Hashtable
A Hashtable uses a key to access the items in the collection. To access elements in a HashTable, you will need a unique key. Each element in a hash table has a key/value combination

Stack
This is a collection object that stored and accessed on a Last In First Out(LIFO) basis. Items can only be accessed and inserted through on end of the stack. Adding an item to the Stack is called pushing while retrieving an item from the stack is called popping

Queue
This is a collection object that stored and accessed on a First In First Out(FIFO) basis.

Collections in Scala
Scala provide a rich set of collection objects. An example is Scala Lists.
Scala Lists are very similar to the Arrays in C#. The elements are of the same data type sore using the same name. Lists in Scala are described as immutable, meaning that the items in the list cannot be changed by assigning a value. Additionally, Scala List are linked lists whereas arrays are linear.
Scalar lists can either be one-dimensional or two-dimensional.

Example of implementation of Scala lists is given below:

//List of string objects
val animals: List[Stirng] = List("cat", "monkey", "rabbit", "cattle")

//List of Integer data
varl numbers = List[Int] = List(0,1,2,3,4,5)

//An empty List
val empty = List[Empty] = List()

//Two dimensional List
List( List(1,2,3), List(2,4,6), List(5,10,15))


Pointers in C#


C# provides a feature for using pointer variables. A pointer variable that points to another variable. Its means that the values of a pointer variable is the address of another variable or memory location. Pointers just like other variables needs to be declared before use. Pointers can also be considered as a data type.

A pointer variable is declared as follows:

type *variable_name;

where type is the data type of the variable
* indicates that it is a pointer variable
variable_name is the name of the variable

Example of pointer declarations are as follows:

double *ki;     //pointer to a double type

int *x ;   //pointer to an integer type

Pointers in Scala


The Scala Programing Language does not provide and implementation for a pointer type.