Creating Classes with Namespaces and Constructors in C#

Organize your code and build powerful, reusable object blueprints

As your applications grow, so should your code organization. That’s where namespaces come in — to group related classes. And when you need to create an object with initial values, you’ll use constructors to do the job.

Together, these tools make your C# projects more structured, maintainable, and scalable.


🗂 What Is a Namespace?

A namespace is like a folder or container for your classes. It:

  • Groups related types (e.g., models, services, UI logic)
  • Avoids name conflicts
  • Keeps your codebase clean

🔧 Syntax Example:

namespace MyApp.Models
{
    public class Product
    {
        public string Name { get; set; }
        public decimal Price { get; set; }
    }
}

✅ Using a Class from a Namespace

Add this at the top of the file where you want to use the class:

using MyApp.Models;

Product p = new Product();

✅ You now access your classes easily from other parts of your app.


🔨 What Is a Constructor?

A constructor is a special method that runs when you create a new object. It’s used to initialise values or run setup logic.

public class Product
{
    public string Name { get; set; }
    public decimal Price { get; set; }

    // Constructor
    public Product(string name, decimal price)
    {
        Name = name;
        Price = price;
    }
}

🧪 Usage:

Product p = new Product("Laptop", 999.99m);

✅ Sets up your object with data immediately.


🔁 Constructor Overloading

You can create multiple constructors with different parameters:

public class Product
{
    public string Name { get; set; }
    public decimal Price { get; set; }

    // Default constructor
    public Product() { }

    // Custom constructor
    public Product(string name)
    {
        Name = name;
        Price = 0;  // default value
    }

    public Product(string name, decimal price)
    {
        Name = name;
        Price = price;
    }
}

✅ Lets you create objects flexibly depending on what data is available.


🧠 WinForms Example

You might want to model something like a user form submission:

namespace MyApp.Models
{
    public class User
    {
        public string FullName { get; }
        public DateTime RegisteredAt { get; }

        public User(string fullName)
        {
            FullName = fullName;
            RegisteredAt = DateTime.Now;
        }
    }
}

Using in your form:

using MyApp.Models;

User u = new User(txtName.Text);
MessageBox.Show($"{u.FullName} registered at {u.RegisteredAt}");

📚 Summary

FeaturePurposeExample
namespaceOrganise and group related classesnamespace MyApp.Models { ... }
constructorInitialise a new object with valuespublic Product(string name)
overloadingProvide multiple ways to construct an objectProduct(string name, decimal)

✅ Best Practices

  • ✅ Use namespaces to mirror folder structure (Models, Services, UI)
  • ✅ Use constructors to enforce required data
  • ✅ Avoid logic-heavy constructors — just initialize
  • ✅ Use default values or overloads for flexibility
  • ✅ Place using statements at the top to access namespaces cleanly

🧪 Quick Challenge

🧩 Create a Book class inside a LibrarySystem.Models namespace with:

  • Properties: Title, Author, Year
  • Constructor that requires all 3
  • Another constructor that defaults Year to DateTime.Now.Year

Create a book object in your form and display its info in a message box.


🎓 Want to Go Further?

  • Learn about static constructors
  • Use namespace aliases for managing conflicts
  • Create nested namespaces (e.g., MyApp.Data.Models)
  • Explore partial classes across multiple files

💬 Need help designing your namespace structure for a real app?
Join us on one of our Object-Oriented C# Course