exercism/csharp/attack-of-the-trolls
Ben Harris 588f26458c create solution and update all to .net 6 2021-11-10 15:09:57 -05:00
..
.exercism some more 2021-11-08 23:55:59 -05:00
AttackOfTheTrolls.cs some more 2021-11-08 23:55:59 -05:00
AttackOfTheTrolls.csproj create solution and update all to .net 6 2021-11-10 15:09:57 -05:00
AttackOfTheTrollsTests.cs some more 2021-11-08 23:55:59 -05:00
HELP.md some more 2021-11-08 23:55:59 -05:00
HINTS.md some more 2021-11-08 23:55:59 -05:00
README.md some more 2021-11-08 23:55:59 -05:00

README.md

Attack of the Trolls

Welcome to Attack of the Trolls on Exercism's C# Track. If you need help running the tests or submitting your code, check out HELP.md. If you get stuck on the exercise, check out HINTS.md, but try and solve it without using those first :)

Introduction

Attributes

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 is 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 is FlagsAttribute by convention, but the suffix Attribute can be omitted when applied on a declaration.

Flag Enums

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's constants are interpreted as bitwise flags and therefor indicates the enum supports the bitwise operators and additional features like the method Enum.HasFlag().

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
}

Instructions

In this exercise you'll be checking permissions of user accounts on an internet forum. The forum supports three different permissions:

  • Read
  • Write
  • Delete

There are three types of accounts, each with different default permissions:

  • Guests: can read posts.
  • Users: can read and write posts.
  • Moderators: can read, write and delete posts, they have all the 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.

1. Get default permissions for an account type

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

2. Grant a permission

Implement the (static) Permissions.Grant() method that grants (adds) a permission:

Permissions.Grant(current: Permission.None, grant: Permission.Read)
// => Permission.Read

3. Revoke a permission

Implement the (static) Permissions.Revoke() method that revokes (removes) a permission:

Permissions.Revoke(current: Permission.Read, grant: Permission.Read)
// => Permission.None

4. Check for a permission

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

Source

Created by

  • @ErikSchierboom

Contributed to by

  • @valentin-p
  • @yzAlvin