Nothing to see...

A simple blog about all things in the world that is ridikulouse.

Technological steps, are man kinds greatest achievements

Not a Fighter, but a lover of Tech.

Love of the internet

The Internet is the final frontier for open connected networks, it promotes speech and advances knowledge for any mere person. The internet is fast becoming a need rather a want, and it is recognised by the UN as a necessity for the modern person.

Photography

Photography is more than just Art and expression, it is the manipulation of the light and provokes emotion and memories.

Have a look around

The articles on this blog represent my thoughts and views at the time of writing, I can always change my views through further education...please don't hold me against my views. Some of the articles have been written to assist anyone else with similar issues - it also helps me to remember. Hope you get something out of this.

Monday, July 20, 2015

String Interpolation

String Interpolation is an interesting method of being able to access properties directly within a string. Once upon a time in order to include properties into a string you would have to do something like this :

int age = 16;
string mystr = "My age is" + age.ToString();

Essentially a string concatenation and then returning other types as a string.

Then we had string.Format which I thought it was fantastic with the autoconversion of types to string - so we had something like this:

string.Format("My age is {0}", age);


Now, this was very handy and made string concatenation simple and easy to read (have you ever encountered someone else's string concatenation and wondered wtf)?


One of the downfalls of string.Format was that you had to remember the order to add the variable. Since string.Format uses a format as {0}, {1}, {2}, etc... means that your arguements will need to be in the exact order. If you have alter your code delete a string block in the middle then you have to re-sequence everything.

String Interpolation aims solve some of these issues by allowing you to use name properties in your string. The code will look something like this :

int age = 16;
string mystr = $"My age {age}";

Notice, the "$" in front of the string, and also note that the property is surrounded by parenthesis.

Null Conditional Operators

I cannot count the number of times that I've faced exception caused by running into a null. This then leads to code using the if{} blocks or using the short hand "?" on almost all of your variables.

In C# 6.0 specification a null conditional operator. The null conditional operator allows you to check for the instance of a null and replace a returned value or to return a value.

For example :

If you have a simple class such as a Person :

public class person
{

public string Name {get;set;}

public int Age {get; set;}

}

and you instantiate the class :

person me = new person();


then if you do something like

Console.Writeline(me.Name);


you'll get a null exception since the Name has not been instantiated. Often to overcome this specific problem we use constructors :

public class person
{

public string Name {get;set;}

public int Age {get; set;}

public person()
{
Name = "";
Age = 0;
}

}

The Null Conditional Operator now allows you to check for the null based on a shorthand, and this is how it would look in use:

Console.Writeline(me?.Name ?? "Empty Name");

So lets look at what this is doing, the first item to look at is "me?.Name" which is essentially checking the object me to see if it is null, if it is not null it will look at the Name property. The second part is '?? "Empty Name"' which is basically stating that if any part of the expression is null then use the string "Empty Name".

This shorthand example is very important and useful in order to make your program less verbose. For example in order to the above single line you would have had to do multiple if{} blocks (first to check that me is not null, and then the Name property is not null.