Using C# keywords in your Enums

Sometimes you want to define a value in your enumeration which has the same name as a keyword in C#.

The compiler will not like this:

The solution in C# for this is to prefix your value with the @-character:

namespace EnumTest
{
 //as = attosecond
 public enum Time : byte
 {
 s = 1,
 ms = 2,
 @as = 3
 }
}

For Visual Basic use square brackets like this [as].

Advertisements

Remove and sort using statements

A lot of people don’t know this but you can actually clean up your using statements in your Visual Studio projects almost automatically.

In case you didn’t know, here is how to do it:

You have a big list of using statements in your class. It might look like this by default:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

Just do a right-mouse click on the using statements and select:
[Organize Usings] > [Remove and Sort].

This will remove all the unnecessary and unused usings. The result might look as clean as this:

using System.Windows.Controls;

Sometimes, nothing could be removed or sorted but in most new class files there are a lot of default usings. It might be good to clean them once in a while, especially if you wrote and removed lots of (test) code in a class.

Happy programming πŸ˜‰

Variable-length parameters in C#

Sometimes you need to call a method with one default parameter and one extra parameter. The next moment you want to call that same method with its default parameter and two or more extra parameters. In other words, you want variable-length parameters.

In Java this can be done by using … inside the parameters(/arguments) list of a method.
In C# you have several options, depending on your logic.

One choice, which will in most cases not work because of your logic, is to use an optional parameter which can be set to null (or a default value). It requires you to check if the number2 variable is null or empty every time:

public void MyMethod(int number1, int? number2 = null)
 {
 //...
 }

 

You can also choose to use method overloading but instead of writing extra code you can use the params keyword (if it fits your logic).

This code below from this simple Console Application shows some basic usage of the params keyword. One main class to call the methods and one class where the methods were implemented:

using System;

namespace VariableParamsTest
{
 class Program
 {
 static void Main(string[] args)
 {
 MyClass test = new MyClass();

 test.DoSomething(1, 2, 3);
 test.DoSomething(9, 8, 7, 6, 5, 4, 3, 2, 1);

 Console.WriteLine();

 test.DoSomethingElse(new int[] { 1, 3, 5 }, new string[] { "hello", "hi", "bonjour", "hallo" });

 Console.WriteLine();

 test.DoSomethingDifferent("monkey", 42, new double[] { 1.0, 1.1, 1.5, 1.6 });

 Console.ReadLine();
 }
 }
}
using System;
using System.Reflection;

namespace VariableParamsTest
{
 public class MyClass
 {
 public void DoSomething(params int[] numbers)
 {
 foreach (int i in numbers)
 Console.WriteLine("parameter: " + i);
 }

 public void DoSomethingElse(params object[] things)
 {
 foreach (object thing in things)
 if (thing is Array)
 foreach (object o in (Array)thing)
 Console.WriteLine("parameter: " + o.ToString());
 }

 public void DoSomethingDifferent(string word, int number, params double[] numbers)
 {
 foreach (object par in MethodBase.GetCurrentMethod().GetParameters())
 Console.WriteLine("parameter type: " + par.ToString());
 }
 }
}

 

Pretty easy, have fun testing it out. πŸ˜‰

More info about params can be found on MSDN.

 

Visual Studio 2010 and .NET Framework 4 Release Candidate

Visual Studio 2010 and the .NET Framework 4.0 Release Candidate (RC) are now available for MSDN subscribers.

The Visual Studio 2010 and .NET Framework 4 Release Candidate (RC) is available to MSDN subscribers on Monday, February 8th, with general availability on February 10th.

[ source ]

CRUD

While I was reading some website development article by Microsoft I stumbled upon a technical term for a few times.

“…, and discussed how to implement CRUD support …”

I had to find out what it means..

CRUD:

  • Create
  • Retrieve
  • Update
  • Delete

If anybody asks you if you have already finished the CRUD support for (for example:) articles on your website.

Remember this one πŸ˜‰

Just a quick reminder:

  • UI = User Interface
  • UX = User Experience

πŸ™‚

update:

CRUD functionality in Visual Studio 2010 beta 2 with the ASP.NET MVC 2.0 template:

πŸ˜‰