Skip to main content

Posts

C# Tutorial - The Readonly Keyword [Beginner]

C# has a ton of keywords, and it is sometimes hard to keep track of them all. One keyword that often gets lost in the shuffle is the readonly keyword, often because many people just group it with the const keyword and leave it at that. Well, the readonly keyword deserves better than that - and so here today we are going to talk about what exactly it does, how it differs from const , and why you would ever want to use it at all. The readonly keyword does exactly what its name states - it makes a field read only. Well, I guess it doesn't quite mean just that - because there is one point in the lifespan of an object that a readonly field can be set. That point is during object initialization. And this fact right here is why readonly differs from const - a const field gets determined at compile time, while a readonly field is determined at runtime. For example: public class MyClass { public const string foo = "foo" ; public MyClass () { ...

C# Tutorial - Singleton Pattern [Beginner]

The singleton is one of the most used and well know design patterns in programming. The idea of a singleton is to have a class which can only be instantiated once. These are very useful for state objects and global resource objects. I will go over several different ways to create singletons and also how to create a generic singleton class. There are several different places you find information on singletons and creating them for all different languages. One great resources is the Gang of Four book, Design Patterns: Elements of Reusable Object-Oriented Software . One of the main concerns when using/creating singletons is multi threaded applications/environments. The issue occurs when two threads decided to get an instance of a never made singleton then they have the chance of creating two instances. All the solutions I will present are thread safe. The first solution presented is a singleton class that you would template your classes after. It creates the singleton in...

C# Tutorial - The => (Lambda) Operator [Beginner]

C# 3 in .NET 3.5 got a number of handy new language features, but the one I'm enjoying the most at the moment is the addition of the => operator (otherwise know as lambda). While this operator does not add any new functionality to the language, it is a great piece of syntactic sugar. So what does this syntactic sugar buy us? Well, in .NET 2.0, writing anonymous functions inline was a very verbose process. The code often looked ugly, and many times where anonymous methods would have served me perfectly well, I resorted to giving the function a name just to make the code look cleaner. No more! With the introduction of the => operator in .NET 3.5, anonymously inline functions now have a very clean look. It is not as clean as some other languages out there (scheme, for instance), but it is more than enough to make anonymous functions feel like they belong in your code, instead of looking like eyesores. So enough of me yammering on about touchy-feely thing like '...

C# Snippet Tutorial - The Params Keyword [Beginner]

We are going to take a quick look at another C# keyword today - the params keyword. This keyword is another one of those not well known keywords in C#, but it makes life a lot easier in certain situations - and code a lot cleaner. What does the params keyword do, you ask? Well, lets take a look. The main use of the params keyword is to give the ability to create functions that take a variable number of arguments - often called "variadic" or "variable arity" functions. This is not a feature specific to C#, and C# is definitely not the first - although it is easier in some languages than others. C and C++ have it, in the form of the "..." syntax, and Java uses that syntax as well. Everything from scheme to javascript can do it - although as you get toward javascript the syntax becomes somewhat ugly (perhaps that is a topic for another tutorial). But this is a tutorial about C#, not about those other languages. So here is some code: public ...

C# Tutorial - XML Serialization [Beginner]

In this tutorial, I'm going to demonstrate how to serialize your own objects to and from an XML file. Since .NET can use reflection to get property names, basic serialization is unbelievably simple. It only gets slightly difficult when you want to name your XML tags differently than your property names (but still not very hard). If you've ever used an XML serialization package in C++ like boost, tinyXML, or libXML2, you'll see how comparatively easy C# is to use. Let's start with a basic example. Below is an object that stores some information about a movie. public class Movie { public string Title { get ; set ; } public int Rating { get ; set ; } public DateTime ReleaseDate { get ; set ; } }   All right, now that we have an object, let's write a function that will save it to XML. static public void SerializeToXML ( Movie movie ) { XmlSerializer serializer = new XmlSerializer ( typeof ( Movie )); ...