C# and its difference with Java part 2

Conversion

In Java, we use a parse method to convert one type of data to another.

An example would be the parseInt() which converts the string representation of the integer to an integer data type.

int x = Integer.parseInt("42");

A similar way exists in C# and some of them are listed below.

  • ToBoolean
  • ToByte
  • ToString
  • ToInt16
  • ToInt32
  • ToInt64

As an example I would use my parseInt() example of turning the String 42 to an integer but instead use the ToInt32 to demonstrate syntax of C# when converting one type of data to another.

int x = Convert.ToInt32("42");

Arrays

Both Java and C# provides the functionality of arrays such as the simple array (int[] x) and jagged array or array of arrays(int[][] x). C# provides another way of creating an array which is a multi-dimensional array (int[,] x). In a glance multidimensional and jagged arrays looks similar functionality-wise but there are still a few difference between the two.

First would be the initialization for a jagged array, each array of the array would need to be initialized more commonly through a loop but with a multidimensional it can be initialized in one statement.

Example of Jagged Array initialization in C#.

int[][] jaggedArray = new int[2][];
for(int i = 0; i <2; i++)
{
jaggedArray[i] = new int[2];
}

Example of Multi-dimensional Array initialization in C#.

int[,] mulArray = new int[2,2];

Another would be they’re use of memory, a jagged array creates an array of object for each array which consumes more memory compared to an multi-dimensional array.

Constants

Another similarity between C# and Java would be their use of constants which are fixed values that may not be changed throughout the program’s execution.

In Java, we use the keyword final.

final int meaningOfLife = 42;

While in C#, we use the keyword const

const int meaningOfLife = 42;

Loops

C# adds another kind of loop to the popular for and while loops which is called foreach. The foreach loop can be used to iterate through a collection such as an array but cannot add or remove any elements on the collection.

The syntax for the foreach loop is:

foreach(<datatype> <variable name> in <collection name>)

An example would be:

int[] arr = new int[4] {1,2,3,4};
int index = 0;
foreach(int i in arr)
{
Console.WriteLine("Value at Index{0}: {1}", index, i);
index += 1;
}

The result of the previous code would be:

Value at Index 0: 1
Value at Index 1: 2
Value at Index 2: 3
Value at Index 3: 4

Nullables

C# provides a special kind of data type called nullables, which allows the variable to be assigned a value within its range and also a null value. This can be useful when working with databases.

The syntax for nullables is shown below.

<datatype>? <variable name>

Example:

int? num1 = null;
int? num2 = 42;
Console.WriteLine("1st Nullable shows: {0}", num1);
Console.WriteLine("2nd Nullable shows: {0}", num2);

The result of the previous code is:

1st Nullable shows:
2nd Nullable shows: 42

Sometimes it might be useful to have a default value when working with nullables which is where the null coalescing operator (??) comes in handy. When the first operand is null it returns the second operand, otherwise it returns the first one.

Example of null coalescing operator.

int? num1 = null;
int? num2 = 42;
int num3;
num3 = num1 ?? 7;
Console.WriteLine("num3 shows: {0}", num3);
num3 = num2 ?? 7;
Console.WriteLine("num3 shows: {0}", num3);

The result of the previous code is:

num3 shows: 7
num3 shows: 42
One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.