Archive for November, 2010

How do I – Use a bitmask enum in C#

Written by Cornelius J. van Dyk on . Posted in How Do I...

In today’s world where processor memory is plentiful, many developers will often times just use a List<bool> for tracking a series of on/off values.  Though the compiler can optimize this code somewhat, the CPU cycles spent managing the List<> structure when super fast bitwise operations could have been done, isn’t really the best way to go.  So when one of my mentees asked about checking values in an enum bitmask, I thought it best to blog the answer for the benefit of others as well.

Let’s assume I’m trying to track the on/off values of say a permissions mask.  I would define my values using the Flags attribute on an enum thus:

[Flags]
public enum Permissions
{
    All = 4,
    Update = 2,
    Read = 1,
    None = 0
}

If I now proceed to define a variable of our enum, I can set some of it’s bit values using the the bitwise AND operator thus:

Permissions perm = Permissions.Read & Permissions.Update;

If I now check for a value in the mask that is NOT turned on, it is done with the bitwise OR operator thus:

if ((perm & Permissions.All) != 0)
{
    //it never gets here
}

The same applies for values that ARE turned on in the mask thus:

if ((perm & Permissions.Update) != 0)
{
    //it gets here
}

In addition, I can turn on more flag altering future check for them thus:

perm = perm & Permissions.All;
if ((perm & Permissions.All) != 0)
{
    //now it gets here
}

The major advantage to using the bitmask enum with bitwise operators is speed.  These operations are done by the processor literally just flipping a single bit.

Happy coding!

 



Cheers
C




image

How do I – Write my own C Sharp IEqualityComparor when System.Collections.Generic.List.Contains(System.Xml.Linq.XNode) fails and does not work as expected

Written by Cornelius J. van Dyk on . Posted in How Do I...

Unfortunately I ran into an issue with the C# List<T> class the other day.  I was in need of checking for a given XML snippet’s existence in an XML document.  I immediately jumped in and tried using Linq2Xml and List<T> to solve the problem.  The way I figured it, I could grab a quick list of XNodes from the XML document and then using the .Contains() method, I could check if the node in the XML snippet exists in the list.  Imagine my surprise when I ran through the code, but for some reason, the .Contains() method did NOT return true even on an identical match.  Weird.  :S

After spending some time trying to figure out why it wasn’t working as advertised (as I’ve mentioned before, 80% of a developer’s time is spent figuring out why something isn’t working as advertised or published) I decided to go the easier route and leverage the .Contains() method’s second overloaded form which takes an IEQualityComparor so all I’d have to do is write my own IEQualityComparor derived class and pass it to the .Contains() method.  Let’s take a look at how we do this.

 

http://crayveon.com/write-c-sharp-iequalitycomparor-system-collections-generic-list-containssystem-xml-linq-xnode-fails/

 

In this post we’ve shown how to create our very own IEqualityComparor method to use for our purposes since the List<XNode>.Contains() method doesn’t function the way we needed it to.  I would venture to say, that the .Contains() method without our IEqualityComparor is pretty much useless.

Of course, if anyone out there can explain to me why it works the way it does and how the way it works is actually useful, I’m all ears. 🙂



Cheers
C




image

Start thinking in parallel… seriously!

Written by Cornelius J. van Dyk on . Posted in Blog

As we move forward with Visual Studio 2010 and the .NET Framework 4.0, it will become more and more important to understand, embrace and implement parallel programming patterns.  Even though SharePoint 2010 still only runs on .NET 3.5, projects and components that doesn’t directly call into SharePoint, can be built upon this framework technology.

With servers now spanning multi proc/multi core (dual proc/hex core for most standard servers today), it means that a traditional single threaded application only leverages 1/24th (12 cores plus hyper-threading) of it’s potential processor power.  Using simple components such as Parallel.For for instance can potentially yield much better performance and throughput to the apps we build.  I’m by no means advocating consuming all 24 cores in your application on the server, but the point is that we should be thinking in parallel mode.

That said, Stephen Toub wrote the nice white paper on the subject.  Yes, it’s 118 pages, but it’s well worth the read, even if just to stimulate peripheral awareness of the parallel shift in coding.  I enjoyed it and I hope you do too! 🙂

The paper is published here:  http://www.microsoft.com/downloads/en/details.aspx?FamilyID=86b3d32b-ad26-4bb8-a3ae-c1637026c3ee&displaylang=en

For my own reference, I’ve also saved it off to my downloads folder just in case the above link isn’t functional some time in the future.

http://www.cjvandyk.com/blog/Downloads/Patterns%20for%20Parallel%20Programming%20-%20Understanding%20and%20Applying%20Parallel%20Patterns%20with%20the%20.NET%20Framework%204.pdf

Cheers
C




image

New SharePoint blogger worth following

Written by Cornelius J. van Dyk on . Posted in Blog

My friend, Jason Himmelstein, recently setup the SharePointLonghorn blog and I’ve been eagerly waiting for him to start cranking out content.  Well the wait is over.  He’s launched into the blogosphere with some awesome posts well worth reading.  While you’re at it, subscribe to his RSS feed.

Windows with Claims User gets access denied to a site they had access to earlier in the day

Unable to access VMware Workstation Guest after Host Machine crash Random Server hang issues result in a required hard reset SharePoint 2010 Farm Service Account passwords expired?!?!?!? Like I said… great content!  Enjoy!

Cheers
C




image