LogIn
I don't have account.

Exploring the General Structure of a C# Program

DevSniper

144 Views

C# is a statically-typed, object-oriented language designed for building scalable and high-performance applications. While many developers are familiar with its syntax, understanding the deeper structure of a C# program. Let’s dive deeper into the structure

1. using Directives

using System;
  • Purpose: Includes the System namespace so you can use types like Console, Math, DateTime, etc., without fully qualifying them.
  • Advanced Tip: Starting with C# 10, you can use global using to reduce repetitive code across files:
    // global usings (C# 10+)
    global using System;

2. Namespace Declaration

namespace <NamespaceName>
{
   // types go here
}
  • Purpose: Organizes your code into logical groups and prevents type name collisions.
  • Best Practice: Use a naming convention based on your project or company hierarchy (e.g., CompanyName.ProductName.Module).
  • C# 10+ Feature: You can also use file-scoped namespaces for cleaner syntax
    namespace MyApp.Utilities;

3. Class

class <ClassName>
{
   // members, methods, properties
}
  • Reference type: Stored on the heap, passed by reference.
  • Used for: Object-oriented design, data encapsulation, services, domain models.

4. Struct

struct <StructName>
{
   // value members
}
  • Value type: Stored on the stack, passed by value.
  • Use when: You need lightweight objects with no inheritance—e.g., points, colors, vectors.
  • ⚠️ Avoid structs with large data (>16 bytes) to prevent unnecessary copying.

5. Interface

interface <IInterfaceName>
{
   void DoSomething();
}
  • Contracts: Define "what" a class should do, without specifying "how".
  • Best used for: Dependency injection, polymorphism, testability.

6. Delegate

delegate void MyDelegate();
  • Function pointer abstraction: Allows you to pass methods as arguments.
  • Key for: Events, callbacks, LINQ and asynchronous programming.

7. Enum

enum <EnumName>
{
   OptionA,
   OptionB
}
  • Strongly typed constants: Great for states, flags, roles, modes.
  • Use [Flags] attribute to combine values via bitwise operations.

8. Nested Namespaces

namespace MyApp
{
    namespace Utilities
    {
        // types here
    }
}
  • Logical grouping: Organize large applications into nested modules.
  • Can be written more compactly as
    namespace MyApp.Utilities
    {
        // ...
    }

Summary

using System;
namespace <NamespaceName>
{
    class <ClassName>
    {
       //......
    }
    struct <StructName>
    {
       //......
    }
    interface <IInterfaceName>
    {
       //......
    }
    delegate <DataType> <DelegateName>();
    enum <EnumName>
    {
      //.....
    }
    namespace <NestedNamespaceName>
    {
       //......
    }
}

Program Entry Point

1. Using top-level statements

Copy
using System;

// Your program starts from here 
Console.WriteLine("Hello world!");

namespace <NamespaceName>
{
    class <ClassName>
    {
       //......
    }
    struct <StructName>
    {
       //......
    }
    interface <IInterfaceName>
    {
       //......
    }
    delegate <DataType> <DelegateName>();
    enum <EnumName>
    {
      //.....
    }
    namespace <NestedNamespaceName>
    {
       //......
    }
}
Hello world!

2. Using Main Method

Copy
using System;
namespace <NamespaceName>
{
    class <ClassName>
    {
       //......
    }
    struct <StructName>
    {
       //......
    }
    interface <IInterfaceName>
    {
       //......
    }
    delegate <DataType> <DelegateName>();
    enum <EnumName>
    {
      //.....
    }
    namespace <NestedNamespaceName>
    {
       //......
    }
   class Program
    {
        static void Main(string[] args)
        {
            //Your program starts from here
            Console.WriteLine("Hello world!");
        }
    }
}
Hello world!