To C sharp is the new black

This update is going to be about what I've been doing recently! What I've been doing is I finally got around to update my windows phone app (although I haven't released it yet as I've still got some issues to resolve). The thing is that I've been using C# for developing my application, hence the title, and I've finally gotten back to C# after about a years pause. In my choice and embracing of languages I've probably been a bit intoxicated by C# being my first OOP language. However, I've used quite a lot of python recently which is a decent language as well, however the package handling could be improved.

Anyway back to taking about the thing I came here to write about. In the process of updating the windows phone app I've been implementing tests cases for some functionality. (I didn't have much time when i first wrote the app, because of a deadline, so I skipped it then). In retrospect I should have implemented them when I first started the app, as coming back to the app after a year it would have been great to have the tests. But oh well...

Some time ago I bought the book C# in depth by Jon Skeet, and I've got around to read 4 chapters so far. Most of the text so far has been a refreshment of what I already knew, but it's a nice book, and I like his writing style. Anyway in the process of reading the book and updating the app, I've grown quite fond of some of the things available in C#. Such as LINQ! The thing that one can turn this:

1
2
3
4
5
6
7
8
9
foreach (UserObject user in IEnumrableResult)
{
    if (user.ObjectId == toCheckObjectId)
    {
        var tmp = new testObject(user.greeting);
        list.Add(tmp);
    }
}
return list;

into this:

1
2
3
return (from UserObject user in IenumerableResult
        where user.ObjectId == toCheckObejctId
        select new testObject(user.greeting)).ToList();

Another thing that I've grown fond if is the ?? operator of C#. The ?? operator can be used for setting appropriate values depending on if something is null. So lets take an example! One can turn the following:

1
2
3
4
5
6
7
get
{
    if(prop == null)
        return new Prop();
    else
        return prop;
}

into this:

1
2
3
//If prop is not null it will be returned.
//If prop is null we create a new Prop object and returns this.
get { return prop ?? (new Prop()); }

So this was a little insight on what I've learned about the C# language lately.
Maybe I will write more but for now I'm off.

- Archi

Category(s): Everyday life
Tags: , , , , ,

Comments are closed.