January 31, 2026

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.

0 0 votes
Article Rating
Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback

[…] The codes above creates a string object, and in this case, “Hello World“. Scala provides methods known as ancestor methods which are used to obtain information about a string. To get the length of a string (that is the number of characters in the string), we use the length() method. The code fragment below shows how we can obtain the length of a string, that is the number of characters making up the string. object Demo {    def main(args: Array[String])     {        var my name = “Kindson The Tech Pro”;        var len = máname.length();        println(“The length of the string is ” + len);    } } The output of the code would be: The length of the string is 20 Concatenation is the joining of two string to yield a larger string. The accessor function to achieve this is concat(). The code fragment below illustrates its use … String1 = “Kindson”; String2 = “Munonye”; String3 = String1.concat(String2); … After this codes executes, String3 would contain KindsonMunonye 8. Functions and Procedures A function is a group of statements that perform a task. You can divide up you code into separate functions. This functions come together to make up the complete program. Functions and Procedures in C# A function in C# allows you to put together a piece of code and call it from other parts of your program. You may encounter a situation where you may have to repeat a piece of code fragment, in multiple parts of your program and this is where functions would be very useful. In C#, functions are basically declared like this: <visibility> <return_type> <function_name> (<parameters>) {      body_of_function } An example of a typical function is given below. public in Max(int a, int b) {        if(a > b)             return a:        else             return b;      } Functions and Procedures in Scala In Scala, there is both functions as well as methods. Methods may also be functions with a little difference. A method in Scala is part of a class that has a name, a definition and some anotations which may be optional Function on the other hand, is a complete object which can be assigned to a variable. Therefore if a function is defined as a member of some class, it can be called a method of that class. To declare a function in Scala, use the syntax: def <function_name> ([argument_list]) : [return_type] =     function body     return [expression] } Continue to Part 2… […]