Enumeration Mapping – Introduction

In many cases, you may have tables for value lists in your database that only hold an integer primary key and a description field. Then when you want to display this description, you’ll have to join the table every time. It is not a problem for dynamic value lists or if they change on a regular basis without the need for new code implementation. But if they are static, the join looks more like a waste of time. One of the most common example is the order state in sales applications. There are only a few values like New, In Progress, Delivered and each value requires some specific code implementation. You could of course decide to load those static data when your application starts and cache them. But Entity Framework has another solution to that issue: enumeration mapping. This allows you to convert any integer field of your model to an enumeration type. This feature has some interesting advantages:

  • You no longer have to join the value list tables every time you want to get the description;
  • You don’t have to implement a caching system for value lists;
  • Enumeration types are much easier to use in code than pure integer values.

Partial members

Partial members appeared very early with C# 2.0. They allow you to dispatch the code of a class or method between many files.

Partial classes are very useful when you want to dispatch groups of methods of a class between many developers and avoid risks of merge conflicts if multiple check-out is enabled. The class is therefore split into multiple files. Imagine for instance a class that manages queries and you want to separate the create, read, update and delete methods. Then you have four files but only one class. But the most common reason why you would use partial classes is when you work with code generators. Indeed when a class is created by a code generator, you shouldn’t modify it, at least not the generated file. So instead of creating inheritance, you may just create a partial class in another file. So how do we declare such a class ? Just add the partial keyword to the class declaration, it is that easy (Figure 1)!

partial-class

Figure 1

Repeat this operation in each file that is part of the partial class. Of course, be aware that all declarations must be located in the same namespace. Note that interfaces can also be partial.

Regarding partial method, you obviously cannot spread the implementation at several locations. A partial method must be part of a partial class and allows you to separate its declaration from its implementation, which is somehow similar to what we do with interfaces. So the essential use of partial methods is when the code is generated. This allows you to declare a method and add calls to it in your generated code (Figure 2), which by default won’t execute anything as the method is like empty and then implement it in a partial class (Figure 3).

partial-method-declaration

Figure 2

partial-method-implementation

Figure 3

Unlike partial classes and interfaces, partial methods have some constraints:

  • They cannot return any result;
  • They cannot have out parameters;
  • They must be private (but no access modifier is allowed anyway).

Using goto in a switch

In a previous post, I explained how to use the evil goto keyword. Here I would like to show you a special use of it. When you are in a switch block and some cases must execute their own actions plus the actions of another case. Then you can chain the goto and case keywords (Figure 1).

goto-case

Figure 1

Each case is actually like a label, meaning that the sequence will directly continue at the case you specify. Here below is the result of the above code (Figure 2).

goto-case-result

Figure 2

Same actions for many cases in a switch

Sometimes you would like to use a switch block in which many cases must execute the same actions. For this purpose, you don’t need to duplicate the code for each case. You may just use the following syntax:


switch (myValue)
{
case myFirstCaseValue:
case mySecondCaseValue:
Actions();
break;
case myThirdAction:
OtherActions();
break;
default:
break;
}

As you can see, you just have to group the cases together (without any break between them of course).

The goto keyword

Just by reading the title, I know that many people will hate me. But as this statement exists in C# I wanted to introduce it even if I honestly never used it in .NET. This is a keyword that was very useful, or even maybe essential in older languages but it then became a developer’s nightmare. C# is one which kept this legacy. This statement allows you to break the sequencential execution of a method and continue elsewhere, based on a label. Labels can be created before or after the goto statement, which means that you can go back in time. Sounds great ? And what if I tell you that this is exactly what is called spaghetti code ? That kind of code quickly becomes hard to maintain. But let’s get back to business anyway with an example. In a console, I’m asking a user to enter a number and, if the value is fine, I tell him/her whether it is an even or an odd number (Figure 1).

goto-step-1

Figure 1

Let’s analyze this code. I start with a first condition to check that the entered value is correct. If it is, then I check whether the number is even or odd. In both cases I use a goto statement followed by a label name. Those labels are defined later in the method followed by a colon and statements to execute. Another label, Start, is defined at the very beginning of the method, and is called when the entered value is not correct. When a goto statement is met, the execution is directly sent to the place where the corresponding label is located. Note that I could have plcaed the statements inside the if and else blocks and the call to Start could have been replaced by a loop. But the goal here is to show you how to use the goto. Let’s see the result when I enter an odd number (Figure 2).

goto-step-1-result-1

Figure 2

Awesome! The execution directly went to the correct statement to display the expected result. Let’s now see the call to the Start label (Figure 3).

goto-step-1-result-2

Figure 3

Once again the result is correct. And finally just for fun, let’s see the result when I enter an even number (Figure 4).

goto-step-1-bad-result

Figure 4

Oops. What the hell happened ? Ah yeah, I forgot to tell you that labels are limited to a single line of code so all the statements that follow do not belong to it. This means that when the execution goes to a label, the program sequentially executes the statement of the label and all the ones that follow it, even if some of them are part of other labels. Thus I have to anticipate this behavior and add a label at the end of the method to which I can go when I’m in the first case (Figure 5).

goto-step-2

Figure 5

I added a label at the end of the method and a statement, because otherwise the code would not build. I then call this label after the Even label display is executed. If I run the application again with the number I used previously, the result is different (Figure 6).

goto-step-2-result

Figure 6

Now that’s better! This time we have the good result in every case. But I hope you now understand why most developers don’t want to use the goto statement. The code quickly becomes harder to read and above all to maintain. That’s why I would advise you not to use it, unless you really like that kind of coding of course.

I will leave it at that now but I’ll write another post to show you how to use the goto in a switch. Because in that case, I think it may be useful and acceptable without creating spaghetti code.

The yield keyword

Here is a keyword I think is underused, probably because it is specific to .NET and thus developers coming from other languages don’t have the reflex to use it. The yield keyword allows you to return a collection from a method without having to declare it nor add the elements that compose it. Intriguing isn’t it ? Let’s see an example to understand this. Imagine I create a method that calculates surfaces based on two even-sized collections received as parameters (Figure 1).

yield-normal-method

Figure 1

So now you can see the declaration of the collection to return, the addition of elements to this collection inside the loop and the return statement at the end of the method. Let’s now see how this same method can be coded by using the yield keyword (Figure 2).

yield

Figure 2

You can directly figure out that the collection declaration is gone, as long as the addition of elements to it of course. Finally, the return instruction is now inside the loop and preceded by the yield keyword. And instead of returning a collection, we directly return the element that was previously added to the collection. The call to this method doesn’t change at all though. I then call it to get all the values and display them in the console (Figure 3).

yield-call

Figure 3

I can finally execute the application and check that the result is the one I expected (Figure 4).

yield-result

Figure 4

Note, however, that this technique can only be applied with an iterator that returns IEnumerable, IEnumerator or one of their generic version.

The continue keyword

We saw in a previous post how to get out of a loop thanks to the break keyword. But let’s now imagine that you just want to skip the current iteration under some condition and continue with the next one. continue ? You know where I’m going ? So let’s take the same example in which we have a loop with a counter from 0 to 9 and displays the counter value in each iteration. The “special” condition to break the normal sequence occurs when the counter is equal to 5 (Figure 1).

continue

Figure 1

With break, the displayed values were 0, 1, 2, 3 et 4. Then the loop just stopped and the execution continued with the first line that follows it. But when we replace it with a continue, we can see that the result is different (Figure 2).

continue-result

Figure 2

We figure out that the value “5” is not displayed but all other values are. Indeed when the condition is met, the code that follows the continue inside the loop is skipped but the execution continues with the next iteration instead of leaving the loop.