Skip to main content

Posts

Showing posts from July, 2013

C# Snippet Tutorial - Performance Timers [Beginner]

Every once in a while, there comes a time when some chunk of code that you have written isn't performing quite as well as you think it should. And when that time comes, you need to have a way of figuring out what parts of your code are causing those performance problems. For the heavy duty cases, there are full blown code profiler programs out there, but for the simpler cases you probably just want to time some segments of code manually. And that is what we are going to take a look at today. A couple of times in various articles in the blog, we have used the value from Environment.TickCount to help determine how long a segment of code took to run. This is quick and painless, but it doesn't give you very high resolution. Even though TickCount here return values that equate to milliseconds, it doesn't actually give you millisecond resolution for time - the best resolution it will give you is 10 milliseconds. For example, I ran the following code on my computer...

C# Snippet Tutorial - The ?? Operator [Beginner]

Just the other day I came across a C# operator that I found particularly useful and decided to share it with everyone here at SOTC - the ?? operator. The briefest explanation is this: ?? is used a lot like the conditional operator (?:), except instead of any condition, it will only check if the value on the left is null. If it is not null, it returns the item on the left - otherwise it will return the thing on the right. We'll start with a really simple example - reference types: string myString = null ; string myOtherString = myString ?? "Something Else" ; Console . WriteLine ( myOtherString ); //Output: "Something Else"   Here we can see a string being assigned to null. Next we use the ?? operator to assign a value to myOtherString . Since myString is null, the ?? operator assigns the value "Something Else" to the string. Let's get a little more complicated and see an example using nullable types. int ? myInt = ...

C# Snippet Tutorial - Static Constructors [Beginner]

I'm going to guess that pretty much everyone who reads this blog knows about constructors and how they work. And I bet everyone knows about static variables and functions as well. But did you know that there is such a thing as a static constructor in C#? Yup, a static constructor. And that's what we are going to take a look at today. The concept behind them is actually pretty simple. A static constructor in C# is a chunk of code that is executed right after all the static variables for a class are initialized. You declare it similar to the way you declare a regular constructor (except that it is declared static). So in many ways, they act exactly like a class constructor, except for statics. Don't worry, if that didn't make perfect sense, there are plenty of examples to come. Lets take a look at a really simple class with a static constructor: public class StaticTest { public static int SomeVarA ; static StaticTest () { SomeVarA = ...

C# Tutorial - Optional and Named Arguments [Beginner]

When C# 4.0 was released, developers got their hands on some syntactic sugar that helps in the readability and maintainability of code - optional arguments and named arguments. Optional Arguments The first concept we'll be looking at today is optional arguments. Let's use optional arguments to help a common maintainability issue - adding another argument to a function. Here's a really basic method that we'll be extending. public void MyFunction ( int a ) { Console . WriteLine ( "Parameter a is: {0}" , a ); } ... MyFunction ( 3 ); // Output: Parameter a is: 3   At some point in the future, as often happens, a developer decides MyFunction needs to do a little more and needs another argument. public void MyFunction ( int a , int b ) { Console . WriteLine ( a ); Console . WriteLine ( b ); }   This introduces a maintainability problem - everywhere that MyFunction is called now needs to supply another argument. What t...

C# Tutorial - Extension Methods [Beginner]

Today, we are going to take a look extension methods, which is a language feature introduced to C# in .NET 3.0. Extension methods are a piece of syntactic sugar that allow you add new functionality to a class that you don't have direct access to. Sound cool yeah? Cause it is. With C# before extension methods, it was common to have classes full of utility methods that mostly just augmented the functionally of some part of the .NET framework. Say, for instance, there were some other things that you thought would be handy to be able to do with strings whenever you wanted - perhaps you were needing to reverse the words in a string relatively often. You would probably write a class like this: public static class StringUtils { public static string ReverseWords ( string str , char sep ) { char temp ; int left = 0 , middle = 0 ; char [] chars = str . ToCharArray (); Array . Reverse ( chars ); for ( int i = 0 ; i <= chars...

C# Tutorial - Serialize Objects to a File [Beginner]

While storing information in memory is great, there comes a time your users will have to shut your application down. This means (probably) that you will need to write information to a file at some point, because you will want to store whatever data was in memory. Today, we are going to take a look at a feature built into .NET called Serialization that makes writing and reading data structures to and from a file extremely easy. For this example, let's say I want to create a program that keeps track of all the cars my friends own. I'm going to create two objects to achieve this: Car and Owner . The Car object will store the make, model, and year of the car. The Owner object will save some information about who owns the car. Each Car object will hold a reference to an Owner object. //information about the car public class Car { private string make ; private string model ; private int year ; private Owner owner ; public Car () ...