Hergine Richemont

Learning just for the fun of it

namespaces in c#

C# Track, Exercism #42 Red vs. Blue: Darwin Style

Relevant link

Github solution: exercism-solutions/red-vs-blue-darwin-style

Exercism #42: C# exercises on Exercism

Learning Goal

Namespaces

Relevant Information

Namespaces are a way to group related code and to avoid name clashes and are generally present in all but the most trivial code base.

Types enclosed in namespaces are referred to outside the scope of the namespace by prefixing the type name with the dot syntax. Alternatively, and more usually, you can place a using directive at the top of the file (or within a namespace) and type can be used without the prefix. Within the same namespace there is no need to qualify type names.

Task #1: Find a more appropriate way of isolating the code of the two teams

Important: The only purpose of the enclosing class is to allow types with the same name to coexist in the program. That is better expressed by using a namespace. Using a namespace is better because it groups related code without forcing static classes, giving you cleaner organization, fewer unnecessary static types, and more flexibility as your project grows.

I replaced the two(2) “public static class” with “namespace” for better code organization.

Task 2: Simplify the naming system

I used at the beging of the file for the name system simplifying:

  • using Red = RedRemoteControlCarTeam;
  • using Blue = BlueRemoteControlCarTeam;

Solution: I created a namespace name “Combined” for CarBuilder class and all the errors disappeared.


Do you usually rely on using directives, or do you ever use fully qualified type names?

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted