From dc34af2b5de78fb7e7f04e982e145d0bd67a3728 Mon Sep 17 00:00:00 2001 From: Laoujin Date: Sat, 31 Jan 2015 22:34:42 +0100 Subject: [CSharp/en]Added [Flags] --- csharp.html.markdown | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'csharp.html.markdown') diff --git a/csharp.html.markdown b/csharp.html.markdown index 61db6a8a..4258a0c6 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -622,6 +622,22 @@ on a new line! ""Wow!"", the masses cried"; public BikeBrand Brand; // After declaring an enum type, we can declare the field of this type + // Decorate an enum with the FlagsAttribute to indicate that multiple values can be switched on + [Flags] // Any class derived from Attribute can be used to decorate types, methods, parameters etc + public enum BikeAccessories + { + None = 0, + Bell = 1, + MudGuards = 2, // need to set the values manually! + Racks = 4, + Lights = 8, + Full = Bell | MudGuards | Racks | Lights + } + + // Usage: aBike.Accessories.HasFlag(Bicycle.BikeAccessories.Bell) + // Before .NET 4: (aBike.Accessories & Bicycle.BikeAccessories.Bell) == Bicycle.BikeAccessories.Bell + public BikeAccessories Accessories { get; set; } + // Static members belong to the type itself rather then specific object. // You can access them without a reference to any object: // Console.WriteLine("Bicycles created: " + Bicycle.bicyclesCreated); @@ -825,7 +841,6 @@ on a new line! ""Wow!"", the masses cried"; ## Topics Not Covered - * Flags * Attributes * Exceptions, Abstraction * ASP.NET (Web Forms/MVC/WebMatrix) -- cgit v1.2.3