A C# Attribute
provides a way to decorate a declaration to associate metadata to: a class, a method, an enum, a field, a property or any other supported declarations.
You can apply an attribute by adding it on the line before the declaration using a ClassAttribute
and a FieldAttribute
:
[Class]
class MyClass
{
[Field]
int myField;
}
This declarative metadata only associates additional structured information to the code and does not modify its behavior, but that metadata is used by other part of the code to change how its target would behave or add, change or remove, restrict some its functionalities.
There are many predefined and reserved attributes, for example: Flags
, Obsolete
, Conditional
, each has a specific that can be looked up on the C# documentation. Note that the full name of an attribute like Flags
The C# enum
type represents a fixed set of named constants (an enumeration).
Normally, one enum
member can only refer to exactly one of those named constants. However, sometimes it is useful to refer to more than one constant. To do so, one can annotate the enum
with the Flags
attribute
A flags enum can be defined as follows (using binary integer notation 0b
):
[Flags]
enum PhoneFeatures
{
Call = 0b00000001,
Text = 0b00000010
}
A PhoneFeatures
instance which value is 0b00000011
has both its Call
and Text
flags set.
By default, the int
type is used for enum member values. One can use a different integer type by specifying the type in the enum declaration:
[Flags]
enum PhoneFeatures : byte
{
Call = 0b00000001,
Text = 0b00000010
}
In this exercise you'll be checking permissions of user accounts on an internet forum. The forum supports three different permissions:
There are three types of accounts, each with different default permissions:
Sometimes individual permissions can be modified, it is possible for example to give a guest account the permission to also write posts or revoking all permissions from an account would result in having none of the permissions.
First, define an AccountType
enum to represent the three account types: Guest
, User
and Moderator
.
Next, define a Permission
enum to represent the three permission types: Read
, Write
, Delete
, and two extra ones: All
for having all permissions and None
for having none of the permissions.
Then implement the (static) Permissions.Default()
method to return the default permissions for a specific account type:
Permissions.Default(AccountType.Guest)
// => Permission.Read
Implement the (static) Permissions.Grant()
method that grants (adds) a permission:
Permissions.Grant(current: Permission.None, grant: Permission.Read)
// => Permission.Read
Implement the (static) Permissions.Revoke()
method that revokes (removes) a permission:
Permissions.Revoke(current: Permission.Read, revoke: Permission.Read)
// => Permission.None
Implement the (static) Permissions.Check()
method that takes the current account's permissions and checks if the account is authorized for a given permission:
Permissions.Check(current: Permission.Write, check: Permission.Read)
// => false
Sign up to Exercism to learn and master C# with 62 concepts, 168 exercises, and real human mentoring, all for free.