Enum.HasFlag method in C# 4.0

Enums in dot net programming is a great facility and we all used it to increase code readability. In earlier version of .NET framework we don’t have any method anything that will check whether a value is assigned to it or not. In C# 4.0 we have new static method called HasFlag which will check that particular value is assigned or not. Let’s take an example for that. First I have created a enum called PaymentType which could have two values Credit Card or Debit Card. Just like following.

public enum PaymentType
{
    DebitCard=1,
    CreditCard=2
}

Now We are going to assigned one of the value to this enum instance and then with the help of HasFlag method we are going to check whether particular value is assigned to enum or not like following.

protected void Page_Load(object sender, EventArgs e)
{
    PaymentType paymentType = PaymentType.CreditCard;

    if (paymentType.HasFlag(PaymentType.DebitCard))
    {
        Response.Write("Process Debit Card");
    }
    if (paymentType.HasFlag(PaymentType.CreditCard))
    {
        Response.Write("Process Credit Card");
    }

}

Now Let’s check out in browser as following.

Enum.Has Flag in C# 4.0

As expected it will print process Credit Card as we have assigned that value to enum. That’s it It’s so simple and cool. Stay tuned for more.. Happy Programming..

Technorati Tags: ,,
Shout it
Published Friday, December 31, 2010 11:33 AM by Jalpesh P. Vadgama

Comments

# re: Enum.HasFlag method in C# 4.0

Friday, December 31, 2010 1:31 AM by red bull hats

That was a great piece of information., I enjoyed reading it..,

[url=http://www.caps2011.com]red bull hats[/url]

# re: Enum.HasFlag method in C# 4.0

Friday, December 31, 2010 4:33 PM by JoelDKraft

Since your enum doesn't have the [Flags] attribute, how would using HasFlag be any different/better than just saying "if (paymentType = PaymentType.CreditCard)"?

I'm curious if I'm missing something!

# Business Dedicated Server » Dedicated Virtual Server Hosting And Reliable Managed Web Site Hosting

Pingback from  Business Dedicated Server » Dedicated Virtual Server Hosting And Reliable Managed Web Site Hosting

# Anybody Can make $500/day Easily w/ CPA offers. – $500/DAY: Kenster’s RAGS 2 RICHES | Script ebooks

Pingback from  Anybody Can make $500/day Easily w/ CPA offers. – $500/DAY: Kenster’s RAGS 2 RICHES  | Script ebooks

# re: Enum.HasFlag method in C# 4.0

Sunday, January 02, 2011 1:36 AM by David Taylor

Nice,

You should have shown how it would have been written before, something like:

if ((paymentType & PaymentType.DebitCard) == PaymentType.DebitCard) ...

So obviously the new method is more readable, nice.  I guess if anyone wanted the same thing pre .NET 4 they can write an extension method with exactly the same signature.

Regards,

David

# re: Enum.HasFlag method in C# 4.0

Sunday, January 02, 2011 4:06 AM by webbes

Thanks for bringing this method to my attention I did not know they've added this one to the framework. Your explanation however is somewhat wrong. This new method is all about checking whether a FLAGGED enum has a specific flagged bit set.

We used to check it like this

if((yourObject.EnumProp & yourEnum.EnumValue) > 0){

 // do something

}

Do reed about flagged enums first.

Cheers.

Wes

# re: Enum.HasFlag method in C# 4.0

Sunday, January 02, 2011 6:59 AM by Jalpesh P. Vadgama

@Joel-this is another way of doing this.

# Vitally Important Stuttering Suggestions You Shouldn’t Pass Up » Health and Fitness Articles

Pingback from  Vitally Important Stuttering Suggestions You Shouldn’t Pass Up » Health and Fitness Articles

# MS Visual Studio 2010 » Blog Archive » ???????????????? ????????????. 30.12.2010

Pingback from  MS Visual Studio 2010  » Blog Archive   » ???????????????? ????????????. 30.12.2010

Leave a Comment

(required) 
(required) 
(optional)
(required)