C# Class and Namespace

This post is the beginning of my journey for learning C#. I will start with the very first program ‘Hello World’ to illustrate the concepts of class and namespace. And then, I will briefly talk about the reference of class libraries which consists of DDL and project reference. In the end, I will clarify the definition of a good program which should have high cohesion and low coupling.

The following content will be discussed:

  • Hello World Program Analyze
  • Reference of Class Library
    • DDL Reference (Black-box Reference)
    • Project Reference (White-box Reference)
  • Dependency

Hello World Program Analysis

Class: Class is the main body constructing the program.
Namespace: Namespace organizes class by tree structures to avoid conflicts between classes with the same name.

// Using namespaces in the program
using System;

namespace ConsoleHelloWorld
{
    // 'Program' and 'Console' are name of classes.
    class Program
    {
        static void Main(string[] args)
        {
            // The program will check 'Console'(class) through namespaces that have been specified.
            Console.WriteLine("Hello World!");
        }
    }
}

Reference of Class Library (DLL Reference)

dll: Dynamic Linking Library

Example: If we want to use the ‘button’ from the namespace of ‘System.Windows.Controls’, we need to include the class library of ‘PresentationFramework.dll’.

NuGet: We can use NuGet to manage and refer some class libraries.


Reference of Class Library (Project Reference)

Project Reference: We can refer dll files or projects defined by other developers.

Method 1: (If we only have the dll file and there exists errors, we cannot modify the dll file directly)

1: Add Reference -> Browse -> Add

Method 2: (If we have the source code and there exists errors, we can add breakpoints to find the root cause and fix them.)

1: Include the project to my solution (Add Existing Project)

2: Add Reference


Dependency

High cohesion means that the class is focused on what it should do.

Low coupling indicates that changing something major in one class should not affect the other.

An excellent program should have high cohesion and low coupling.