{"id":276,"date":"2017-06-12T17:51:00","date_gmt":"2017-06-12T17:51:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/2017\/06\/12\/compare-c-and-scala-programming-languages-part-2\/"},"modified":"2019-02-19T14:53:35","modified_gmt":"2019-02-19T13:53:35","slug":"compare-c-and-scala-programming-languages-part-2","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/compare-c-and-scala-programming-languages-part-2\/","title":{"rendered":"Compare C# and Scala Programming Languages &#8211; Part 2"},"content":{"rendered":"<p>This is the second part of our comparison between Scala and C# Programming Languages<\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<div style=\"font-family: 'segoe ui';\">In this part we are going to examine:<\/div>\n<ul style=\"font-family: 'segoe ui';\">\n<li>Arrays Declaration and Manipulation<\/li>\n<li>Collections(List and Sets)<\/li>\n<li>Pointers<\/li>\n<\/ul>\n<div style=\"font-family: 'segoe ui';\">Then we would look at Object Oriented Programming Concepts:<\/div>\n<div style=\"font-family: 'segoe ui';\">\n<p>&nbsp;<\/p>\n<ul>\n<li>Polymorphism<\/li>\n<li>Inheritance<\/li>\n<li>Encapsulation<\/li>\n<\/ul>\n<\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<h3 style=\"font-family: 'segoe ui';\"><b>Arrays<\/b><\/h3>\n<hr style=\"font-family: 'segoe ui';\" \/>\n<div style=\"font-family: 'segoe ui';\">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.<\/div>\n<div style=\"font-family: 'segoe ui';\">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.<\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\"><b>Arrays Declaration and Manipulation in C#<\/b><\/div>\n<div style=\"font-family: 'segoe ui';\">To declare an array in C#, you can use the following syntax:<\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<p><span style=\"font-family: 'courier new' , 'courier' , monospace;\">datatype[] arrayName;<\/span><\/p>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\">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.<\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\">[ ] is used to specify the size of the array<\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\">arrayName specifies the name of the array<\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\">Using the student scores example, we have declare an array as follows:<\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<p><span style=\"font-family: 'courier new' , 'courier' , monospace;\">int[ ] Scores;<\/span><\/p>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\"><b>Initializing an Array in C#<\/b><\/div>\n<div style=\"font-family: 'segoe ui';\">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:<\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<p><span style=\"font-family: 'courier new' , 'courier' , monospace;\">double[ ] Scores = new double[10]<\/span><\/p>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\"><b>Assigning Values to an Array<\/b><\/div>\n<div style=\"font-family: 'segoe ui';\">Values can be assigned to individual elements of an array.. This is achieved by using the index number (also called subscript) as shown below:<\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<p><span style=\"font-family: 'courier new' , 'courier' , monospace;\">double[ ] Scores = new double[10];<\/span><br \/>\n<span style=\"font-family: 'courier new' , 'courier' , monospace;\">Score[0] = 80.5;<\/span><br \/>\n<span style=\"font-family: 'courier new' , 'courier' , monospace;\">Score[1] = 65.0;<\/span><\/p>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\">You can also assign values to the array at the time it is declares as shown below:<\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<p><span style=\"font-family: 'courier new' , 'courier' , monospace;\">double[ ] Scores = {80.5, 65.0, 70, 93.6}<\/span><\/p>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\">The code above declares an array called Scores and assigns 4 elements to the array.<\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\">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;<\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\"><b>Accessing Array Elements in C#<\/b><\/div>\n<div style=\"font-family: 'segoe ui';\">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:<\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<p><span style=\"font-family: 'courier new' , 'courier' , monospace;\">double MaxScore = Score[5];<\/span><\/p>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\">This code retrieves that value of the array element Score[5] and stores it into a variable name MaxScore.<\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\"><b>Array Declaration and Manipulation in Scala<\/b><\/div>\n<div style=\"font-family: 'segoe ui';\">Arrays in Scala share the similar meaning and behavior with C# so lets consider Array Declaration, Array Initialization and Access in Scala.<\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\">Declaring an Array Variable in Scala<\/div>\n<div style=\"font-family: 'segoe ui';\">The syntax for declaring an array variable in Scala is:<\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<p><span style=\"font-family: 'courier new' , 'courier' , monospace;\">var x:Array[String] = new Array[String](10)<\/span><\/p>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\">or<\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<p><span style=\"font-family: 'courier new' , 'courier' , monospace;\">var x = new Array[String](10)<\/span><\/p>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\">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:<\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<p><span style=\"font-family: 'courier new' , 'courier' , monospace;\">x(1) = &#8220;Kindson&#8221;;<\/span><br \/>\n<span style=\"font-family: 'courier new' , 'courier' , monospace;\">x(2) = &#8220;Oleander&#8221;;<\/span><br \/>\n<span style=\"font-family: 'courier new' , 'courier' , monospace;\">x(3) = &#8220;Othniel&#8221;;<\/span><\/p>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\">and so on.<\/div>\n<div style=\"font-family: 'segoe ui';\">You can also assign values as follows<\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<p><span style=\"font-family: 'courier new' , 'courier' , monospace;\">var x = Array(&#8220;Osondu&#8221;, &#8220;Jackasi&#8221;, &#8220;Adaku&#8221;, &#8220;Othniel&#8221;, &#8220;Kindson&#8221;)<\/span><\/p>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<h3 style=\"font-family: 'segoe ui';\"><b>Collections<\/b><\/h3>\n<hr style=\"font-family: 'segoe ui';\" \/>\n<div style=\"font-family: 'segoe ui';\">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.<\/div>\n<div style=\"font-family: 'segoe ui';\">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.<\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\"><b>Collections in C#<\/b><\/div>\n<div style=\"font-family: 'segoe ui';\">Four of the collections classes in C# are outlined below.<\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\"><b>ArrayList<\/b><\/div>\n<div style=\"font-family: 'segoe ui';\">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.<\/div>\n<div style=\"font-family: 'segoe ui';\">An ArrayList supports dynamic memory allocation.<\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\"><b>SortedList<\/b><\/div>\n<div style=\"font-family: 'segoe ui';\">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.<\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\"><b>Hashtable<\/b><\/div>\n<div style=\"font-family: 'segoe ui';\">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<\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\"><b>Stack<\/b><\/div>\n<div style=\"font-family: 'segoe ui';\">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<\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\"><b>Queue<\/b><\/div>\n<div style=\"font-family: 'segoe ui';\">This is a collection object that stored and accessed on a First In First Out(FIFO) basis.<\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\"><b>Collections in Scala<\/b><\/div>\n<div style=\"font-family: 'segoe ui';\">Scala provide a rich set of collection objects. An example is Scala Lists.<\/div>\n<div style=\"font-family: 'segoe ui';\">\n<div style=\"font-family: 'segoe ui';\">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.<\/div>\n<div style=\"font-family: 'segoe ui';\">Scalar lists can either be one-dimensional or two-dimensional.<\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\">Example of implementation of Scala lists is given below:<\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<p><span style=\"font-family: Courier New, Courier, monospace;\">\/\/List of string objects<\/span><br \/>\n<span style=\"font-family: Courier New, Courier, monospace;\">val animals: List[Stirng] = List(&#8220;cat&#8221;, &#8220;monkey&#8221;, &#8220;rabbit&#8221;, &#8220;cattle&#8221;)<\/span><br \/>\n<span style=\"font-family: Courier New, Courier, monospace;\"><br \/>\n<\/span><span style=\"font-family: Courier New, Courier, monospace;\">\/\/List of Integer data<\/span><br \/>\n<span style=\"font-family: Courier New, Courier, monospace;\">varl numbers = List[Int] = List(0,1,2,3,4,5)<\/span><br \/>\n<span style=\"font-family: Courier New, Courier, monospace;\"><br \/>\n<\/span><span style=\"font-family: Courier New, Courier, monospace;\">\/\/An empty List<\/span><br \/>\n<span style=\"font-family: Courier New, Courier, monospace;\">val empty = List[Empty] = List()<\/span><br \/>\n<span style=\"font-family: Courier New, Courier, monospace;\"><br \/>\n<\/span><span style=\"font-family: Courier New, Courier, monospace;\">\/\/Two dimensional List<\/span><br \/>\n<span style=\"font-family: Courier New, Courier, monospace;\">List( List(1,2,3), List(2,4,6), List(5,10,15))<\/span><\/p>\n<\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<h3 style=\"font-family: 'segoe ui';\"><b>Pointers in C#<\/b><\/h3>\n<hr style=\"font-family: 'segoe ui';\" \/>\n<div style=\"font-family: 'segoe ui';\">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.<\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\">A pointer variable is declared as follows:<\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\">type *variable_name;<\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\">where type is the data type of the variable<\/div>\n<div style=\"font-family: 'segoe ui';\">* indicates that it is a pointer variable<\/div>\n<div style=\"font-family: 'segoe ui';\">variable_name is the name of the variable<\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\">Example of pointer declarations are as follows:<\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<p><span style=\"font-family: 'courier new' , 'courier' , monospace;\">double *ki; \u00a0 \u00a0 \/\/pointer to a double type<\/span><br \/>\n<span style=\"font-family: 'courier new' , 'courier' , monospace;\"><br \/>\n<\/span><span style=\"font-family: 'courier new' , 'courier' , monospace;\">int *x ; \u00a0 \/\/pointer to an integer type<\/span><\/p>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<h3 style=\"font-family: 'segoe ui';\"><b>Pointers in Scala<\/b><\/h3>\n<hr style=\"font-family: 'segoe ui';\" \/>\n<div style=\"font-family: 'segoe ui';\">The Scala Programing Language does not provide and implementation for a pointer type.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is the second part of our comparison between Scala and C# Programming Languages &nbsp; In this part we are going to examine: Arrays Declaration &hellip; <\/p>\n","protected":false},"author":2,"featured_media":719,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[414],"tags":[],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/276"}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=276"}],"version-history":[{"count":2,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/276\/revisions"}],"predecessor-version":[{"id":721,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/276\/revisions\/721"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media\/719"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}