Evolution of C Sharp Language: Picture Blog

I was working on something and suddenly a mail popped up. I did not know the sender but I found he/she was a job aspirant. I found him/her very impatient and gist of his/her mail was, “Sir tomorrow I got a job interview. Could you please help me with different versions of C Sharp with specific features in each versions? I just want to know the features name “

I found this question very interesting. Since majority of the time we focus on implementation of a feature. We discuss how a particular feature can be implemented in greater extent but we tend to forget in which version of C Sharp that particular feature got introduced. For a junior level programmer interview, this could be a vital question that list different features of C Sharp version wise. I am trying to answer this question pictorially below,

image

I am sure taking a high level look on this picture before going to interview could be a help. I hope you find this picture post useful. Thanks for reading.

Method Caller Information in C Sharp 5.0

Before we understand Method Caller Information feature of C# 5.0, we need to understand Optional Parameter feature of C# 4.0.

C Sharp 4.0 introduced Optional Parameter feature . According to this feature, in function call only required parameters are needed to be passed. If value of Optional Parameters are not provided then they will take default constant value. To understand it better, let us take following function as example.


static void Display(string Name, int age,string Nationality="Indian")
{

Console.WriteLine("Hey " + Name + " you are " + age + " old " + Nationality);
}

In function Display, Nationality is an optional parameter. So while calling this function you will get intellisense suggesting you that if not provided parameter Nationality will take constant string value “Indian”

image

You can call Display function omitting value of parameter Nationality as following

static void Main(string[] args)
{
Display("dj", 28);
Console.ReadKey(true);

}

In output you will notice that value of parameter Nationality is set to default string constant “Indian”

image 
Now you can see that Optional Parameters has taken the default constant value. C# 5.0 gives us liberty or allow us to attribute optional parameter with caller info attributes. By providing caller info attributes at the runtime optional parameter value will be replaced by dynamic values for caller info attributes. There are three caller info attributes are available,

image

Let us go ahead and rewrite Display function with Method Caller Information attribute on optional parameters.

image

You see in above function definition [see the rectangle], we have attributed three optional parameters with method caller information. We can display method caller information as following,


static void Display(string Name,
int age,
string Nationality="Indian",
[CallerMemberName] string Caller = "",
[CallerFilePath] string file="",
[CallerLineNumber] int line=0)
{

Console.WriteLine("Hey " + Name + " you are " + age + " old " + Nationality);
Console.WriteLine("This function is called  from member " + Caller + " file " + file + " line " + line);
}

Now when you call the function you will get method caller information printed. Expected output would be as following

image 
Method Caller Information is particularly very useful in logging of the application. For high level logging, I would consider using Method Caller Information attributes along with optional parameters. I hope you find this post on Method Caller Information in C# 5.0 useful. Thanks for reading.

What is Namespace in WinJS

Let us first try to understand, why we need Namespace? I will try to explain you with an example, let us suppose you have a function named GreetingMessage in JS file named Library.js

image

Now you need to use this function on default.js. When you try to call that function from other js file , you get following exception.

 

image

Essentially, you cannot access a function or variable outside its scope. Namespace in WinJS library allows you to access a function outside its scope.

  • Namespace allows you to access a function outside its scope.
  • Namespace allows you to use a function in different files along with file it is defined.
  • Namespace helps you to organize your code
  • Any type created in Namespace can be referenced outside the scope of Namespace

Now let us see that how can we define a Namespace in WinJs . You can define Namespace by calling the define function

 

image

There are two parameters need to be passed to create Namespace.

  1. Namespace name is a string
  2. Members of the Namespace as object

You can create a Namespace in Library.js as following,


(function () {

 function GreetingMessage() {

return "Hello from WinJS";

 }

WinJS.Namespace.define("MyLibrary.Function", {
 GreetingMessage: GreetingMessage
 });

})();

There are few points you need to take care here

  • Enclosed all the Namespace definition in self executed function
  • Make sure you are not forgetting the braces at the end.
  • We have defined a function named GreetingMessage
  • We have defined a namespace and function inside that can be accessed as following

image

We have defined a Namespace and now we want to access the function GreetingMessage() on the default.js file. To do this first step you need to do is to refer MyLibrary.js on default.html file. You can refer that as following,

image 
Now you can access GreetingMessage() function on default.js as following ,

var res = MyLibrary.Function.GreetingMessage();
 var output = document.getElementById("output");
 output.innerHTML = res;

In this way by using Namespace, you can access functions and objects in different files and beyond scope of the declaration. I hope you find this post useful. Thanks for reading.

What is Associative Array in JavaScript?

In this post I discussed about Arrays in JavaScript. We access elements of JavaScript array using numeric index. For example, if you want to access 3rd element of a JavaScript array, you will do that as following

image

If you want to traverse through each element of the array you can do that in loop. JavaScript also allows you to store and access elements in the array in using string index than numeric index.

image

You can create Associative Array in JavaScript as following,

image

We are assigning values in array using string index. You can access a particular element with string index name. We can access name information from student information as following

image

One problem working with Associative Array is that we can not traverse through array elements in a loop. Associative Array can be best utilized with Array with Arrays is JavaScript. Read here about Array of Arrays

Let us take above example of student array. If we want more than one student to be stored then, we can create student array as following

image

Then we can create associative Student Array as following

image

Now if push student in arrayofStudent array.

image

If we want to access name of the first student from the array, we can do that as following,

image

For your reference code is given below we used in explaining Associative Array.

 <script type="text/javascript" >
 var arrayofStudent=[];
 var student1 = [];
 student1["rollnumber"] = 19;
 student1["name"] = "John";
 student1["grade"] = "A";
 arrayofStudent.push(student1);
 var student2 = [];
 student2["rollnumber"] = 11;
 student2["name"] = "David";
 student2["grade"] = "B";
 arrayofStudent.push(student2);
 console.log(arrayofStudent[0]["name"]);
 console.log(arrayofStudent[1]["name"]);

<span style="font-size: 12pt;"></span>

</script>

<span style="font-size: 12pt;"></span>


In this way you can work with Associative Array in JavaScript. I hope you find this post useful. Thanks for reading. 

Working with Array of Arrays in JavaScript

 

If you have worked with languages like C, C++ or C# then you must be aware with the concept of multidimensional array. Multidimensional array implies arrays of array. In a two dimensional array, there would be rows and columns. A 3*5 two dimensional array can be shown as below,

0,0 0,1 0,2
1,0 1,1 1,2
2,0 2,1 2,2
3,0 3,1 3,2
4,0 4,1 4,1

Even though you cannot declare a multidimensional array directly in JavaScript though you can have Arrays of Array in JavaScript.

You can define a two dimensional array as following in JavaScript

 

image 

In above code

  • First we defined a base array
  • Then we created two chid arrays. These arrays will constitute different rows of the base array.
  • Each child array is of different size.
  • Assigning each chid array as one of the rows of base array. 

In last line of above code listing, we are printing value of 1st row and 4th column of base array. We will get output as following,

image 
On noticing above code listing you will find that chid array is of different size. In JavaScript chid arrays can be of different size. Essentially you can have three dimensional array in JavaScript as well. You can define that as following code listing,

image

In above code listing,

  • We are defining array named grandparents. This is the base array.
  • We are defining array named parents. Grandparents can have parents.
  • We are defining array names child. Parents can have child.

We want to print “steve”. Steve is second element of child2 array. child2 array is second element of parents1 array. parents1 array is first element of grandparents array. so we can fetch steve with following index value on grandparents array

image 

As output you will get steve printed as following,

image 
For your reference code of two dimensional array and three dimensional array in JavaScript is given below,


<script type="text/javascript" >
 //Two Dimensional Array
 var baseArray = [];
 var chidArray1 = ["a", "b", "c", "d", "e"];
 var chidArray2 = ["1","2","3","4","5","6","7","8"];
 baseArray[0] = chidArray1;
 baseArray[1] = chidArray2;
 console.log(baseArray[0][3]);
 //Three Dimensional Array
 var grandparents = [];
 var parents1 = [];
 var parents2 = [];
 var child1 = ["john", "ram"];
 var child2 = ["mona", "steve", "mark"];
 var child3 = [];
 var child4 = ["chris"];
 parents1[0] = child1;
 parents1[1] = child2;
 parents2[0] = child3;
 parents2[1] = child4;
 grandparents[0] = parents1;
 grandparents[1] = parents2;
 console.log("printing Steve");
 console.log(grandparents[0][1][1]);

</script>


In this way you can work with Arrays of Array in JavaScript. I hope you find this post useful. Thanks for reading.

 

Sorting of JavaScript Array

Does not matter what language you are working on, you will always find Sorting as an essential part of the language. In this post we will focus on Sorting in JavaScript. Sorting is a technique to arrange elements in certain order.

Let us suppose you have an array as following,

image

You will get array elements printed as following

image

If you want to sort this array, JavaScript allows you to sort an array very simply by calling sort function on array. See the listing below, 

image

And now when you print array elements you will elements sorted in alphabetical order 

image

Code for reference is following,


<span style="font-size: 14pt;"></span>

<script type="text/javascript" >

<span style="font-size: 14pt;"></span>

var fruits = ["mango", "lichi", "banana", "Apple"];
 console.log("Before Sorting ");
 for (var i = 0 ; i < fruits.length ; i++) {

<span style="font-size: 14pt;"></span>

console.log(fruits[i]);
 }

 fruits.sort();

<span style="font-size: 14pt;"></span>

console.log("After Sorting");
 for (var i = 0 ; i < fruits.length ; i++) {

<span style="font-size: 14pt;"></span>

console.log(fruits[i]);
 }

 </script>

<span style="font-size: 14pt;"></span>

Let us assume you have an integer array then you can sort that as following


<span style="font-size: 12pt;"></span>

var marks = [89, 45, 67, 39, 99, 58, 44, 92];
 var sortedmarks = marks.sort();
 for (var i = 0 ; i < sortedmarks.length ; i++) {

<span style="font-size: 12pt;"></span>

console.log(sortedmarks[i]);
 }

<span style="font-size: 12pt;"></span>

You will get sorted array as following

image

But wait for a while let us assume array as following

image

Now if you try to sort this array, you will get sorted order as following. This sort order is not expected

image

image

You can solve this problem by passing array reference. JavaScript allows you to pass a function reference as input parameter of sort function.

image

To sort a numeric array, let us create a Compassion function and pass as input parameter to sort method. Compare method is as following

image

And now pass this method to sort function

image

Sorted array would be as following

image

If you want to sort numeric array in descending order you can do that by modifying compare method as following

image

For your reference to code to sort an integer array is given below

<script type="text/javascript" >

var marks = [714,89, 45, 67, 39, 99, 58, 44, 92,100,234];
 var sortedmarks = marks.sort(CompareA);
 for (var i = 0 ; i < sortedmarks.length ; i++) {

console.log(sortedmarks[i]);
 }
 var sortedmarksD = marks.sort(CompareD);
 for (var i = 0 ; i < sortedmarksD.length ; i++) {

console.log(sortedmarksD[i]);
 }

function CompareD(a,b)
 {
 return b-a;
 }

function CompareA(a, b) {
 return a - b;
 }

 </script>

In this way you can work with sorting of Array in JavaScript. I hope you find this post useful. Thanks for reading.

For-in loop in JavaScript Array

   

For-in loop is used to iterate through an object. Let is suppose you have an object as following

 

image

You can iterate through object properties using for-in loop. You can iterate as following listing,

 

image

You will get output as following,

image

In other way you can for-in loop with an array. However that is not advisable. Since sometime for-in loop does not iterate an array in correct sequence of index. But let us go brave here and see, how we could use for-in loop with an array.

Let us assume that there is an array as given in below listing

 

image

You can see that size of the array is 6 and 4t and 5th element of the array is not assigned. Using for loop you can traverse the array as following

 

image  

In loop we are traversing each element one by one and printing the value of element. You see that value of 4th element and 5th element also got printed with value undefined. And it was expected since we did not assign value at these indexes of array.

 

image

Now if you traverse the same array using for-in, you will get only value of the assigned element in the array. For-in skips the unassigned elements while traversing. If you traverse above array as using for-in as following,

 

image

You will get array element traversed as following. You see that now unassigned array elements has been skipped.

image

So you see that in this way you can use for-in loop in JavaScript. I hope you find this post useful. Thanks for reading.