How to Initialize Flags Enumerations in C#

2 Comments »

I have a friend who is just starting to learn C#, so I am getting some interesting questions whose answers seem obvious to me but apparently not to beginners.  Today I answer the question: What’s the best way to initialize Flags enumerations in C#?

As a quick review, here is how Microsoft describes the Flags enumeration:  “You can use an enumeration type to define bit flags, which enables an instance of the enumeration type to store any combination of the values that are defined in the enumerator list.  (Of course, some combinations may not be meaningful or allowed in your program code.)  You create a bit flags enum by applying the System.FlagsAttribute attribute and defining the values appropriately so that AND, OR, NOT and XOR bitwise operations can be performed on them.”

In other words, each enumeration value must correspond to a single, unique bit.  So one way to initialize flags is to use integers that are a power of 2.  The disadvantage of this method is it’s not easy to see which bit is being set, and errors might creep in enumerations with many flags, especially when you start getting up into the range of 16384, 32768, 65536, and 131072.  But there is nothing inherently wrong with this approach either:

[Flags]
public enum DaysOfTheWeek
{
	None = 0,
	Sunday = 1,
	Monday = 2,
	Tuesday = 4,
	Wednesday = 8,
	Thursday = 16,
	Friday = 32,
	Saturday = 64,
}

Read the rest of this entry »

Zoom the View in Visual Studio

No Comments »

I was using Visual Studio on my laptop when suddenly the font in the code editor got so small I could barely read it.  I figured I must have engaged some obscure key sequence, so I did a little research and discovered there are many ways to change the zoom level in Visual Studio:

Keyboard Shortcuts

  • To make the font larger, press CTRL+SHIFT+PERIOD
  • To make the font smaller, press CTRL+SHIFT+COMMA

Keyboard+Mouse Shortcuts

  • To make the font larger, press & hold the CTRL key while scrolling the mouse wheel up one click
  • To make the font smaller, press & hold the CTRL key while scrolling the mouse wheel down one click

Note this also works with the trackpad on most laptops.  Sliding your finger up or down along the right edge of the trackpad is the same as scrolling the mouse wheel.  By the way, I had the CTRL key pressed while I slid my finger down along the right edge of the trackpad, and this is how I accidentally made the font really small.  Also note that each click increases or decreases the font size by 10%.

Read the rest of this entry »

C# Modeling Tools

1 Comment »

C# modeling tools help you model, visualize, analyze, understand and document C# source code.  Most modeling tools use the Unified Modeling Language (UML), which is a standardized way to create visual models from object-oriented source code.

Following is a list of modeling tools that run on Microsoft Windows and model software written with the C# programming language.  Prices listed are direct from the manufacturer for a single license of the “Professional Version” which includes C# round-trip engineering.  This means the modeling tool can read C# source code and generate models, allow the user to make changes to the model, then automatically generate the updated C# code.  Foreign prices are converted to U.S. Dollars at the current exchange rate. 

Please comment with any corrections or additions, as I will keep this list updated.

Read the rest of this entry »

I’m Back!

5 Comments »

Theresa and Timm Martin in front of Half Dome in Yosemite National Park, California

My wife and I have returned from a one-year RV trip across western USA, Canada and Alaska.  It was an amazing trip, truly the adventure of a lifetime.  Although this has nothing to do with C#, if you are interested, please check out our photo tour at National Park Explorers and our behind-the-scenes blog at TNTRV.

Now that I am back working, I will start updating this C# 411 blog again on a regular basis.  Please comment below if there are any C# subjects on which you’d like me to write an article.

Google Considered C# as the Native Language for Android

4 Comments »

Wow, I nearly fell out of my chair when I read this little gem on TechCrunch:

Android chief Andy Rubin wrote in a 2005 email, “If Sun doesn’t want to work with us, we have two options: 1) Abandon our work and adopt MSFT CLR VM and C# language – or – 2) Do Java anyway and defend our decision, perhaps making enemies along the way.”

Imagine how different the world would be today if Google had chosen .NET instead of Java as the native development framework for the Android mobile operating system…

Read more at DevTopics >>

C# Custom Enumerators Made Simple with the Yield Keyword

2 Comments »

An enumerator enables you to iterate over a collection in a foreach loop.  You can use foreach to iterate over all C# collection classes, because all C# collection classes inherit from the IEnumerable interface (regular or generic).  IEnumerable contains the GetEnumerator method, which returns an enumerator.

Occasionally you may find a need to create a custom enumerator, which used to be somewhat of a challenge until the yield keyword was introduced.  Here is how Microsoft describes yield:

The yield keyword signals to the compiler that the method in which it appears is an iterator block.  The compiler generates a class to implement the behavior that is expressed in the iterator block.  In the iterator block, the yield keyword is used together with the return keyword to provide a value to the enumerator object.  This is the value that is returned, for example, in each loop of a foreach statement.

So rather than creating your own enumerator class and managing the enumeration state — which is time consuming and tricky — you can simply write the enumeration logic in the GetEnumerator method, and the yield keyword will automagically wrap your code in a handy-dandy enumerator.

Read the rest of this entry »

.NET Isn’t Dead

2 Comments »

I jump into the controversy about the future of the .NET Framework and HTML5+JavaScript.

Read “.NET Isn’t Dead” on DevTopics.com >>

Apparently C# Doesn’t Suck

2 Comments »

YourLanguageSucks is a wiki on theory.org that lists reasons why the most popular programming languages suck.  There are long lists of reasons why Java, JavaScript, C++ and PHP suck.  But the list for C# is very short:

  • Supports ‘goto’.
  • Two distinct sets of collections: non-generic and generic.  Stack and Queue have the same name in both their generic and non-generic flavors, but then we have Hashtable (non-generic) and Dictionary (generic).

The first reason is easy to discount: just avoid using goto!  The second reason is valid, but not really an issue if you use only generic collections, as I do.

Read the rest on DevTopics >>

Microsoft.CSharp.targets was not found

1 Comment »

If you receive the following Visual Studio compiler error:

Error MSB4019: The imported project "C:Microsoft.CSharp.targets" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.

Read the rest of this entry »

Ultimate List of Freely Available .NET Libraries

No Comments »

Qink has assembled a comprehensive list of freely-available libraries for the Microsoft .NET platform.

Free .NET Libraries

keep looking »