✔ Simple code structure question

Let's say I have interface "IShapes" in file Shapes.cs Now, I'd like to create 3 more classes. Circle, square, and triangle. Should I create 3 more files for each class or rather put them all under the Shapes.cs file?
4 Replies
Angius
Angius9mo ago
That's the convention, yes One class per file, file named like the class (or interface, or enum, or record... you get the idea) So IShape.cs, Circle.cs, Square.cs and Triangle.cs in your case That contain, respectively, interface IShape, class Circle, class Square and class Triangle
Graphics in 2013
What is the point of namespaces then? From what I've learnt, they are used to group classes together (though my notion of namespaces might be wrong, I'd be delighted to know what their real use is)
Angius
Angius9mo ago
Yes, that is the use of a namespace And namespaces usually follow the folder structure So in a
MyApp
|— Services
| |— ShapeService.cs
| |— UserService.cs
| \— BlogpostService.cs
|— Models
| |— Shape.cs
| |— User.cs
| \— Blogpost.cs
\— Program.cs
MyApp
|— Services
| |— ShapeService.cs
| |— UserService.cs
| \— BlogpostService.cs
|— Models
| |— Shape.cs
| |— User.cs
| \— Blogpost.cs
\— Program.cs
you would have
namespace MyApp.Services;
public class ShapeService {}
namespace MyApp.Services;
public class ShapeService {}
namespace MyApp.Services;
public class UserService {}
namespace MyApp.Services;
public class UserService {}
namespace MyApp.Services;
public class BlogpostService {}
namespace MyApp.Services;
public class BlogpostService {}
namespace MyApp.Models;
public class Shape {}
namespace MyApp.Models;
public class Shape {}
namespace MyApp.Models;
public class User {}
namespace MyApp.Models;
public class User {}
namespace MyApp.Models;
public class Blogpost {}
namespace MyApp.Models;
public class Blogpost {}
namespace MyApp;
public class Program {}
namespace MyApp;
public class Program {}
Graphics in 2013
Ah, well, I thought that namespaces could only apply to one class, thank you for clearing that up!
Want results from more Discord servers?
Add your server
More Posts
❔ How can I provide a safe zero cost abstraction over passing managed function pointers over FFII have the following: ```cs public sealed unsafe partial class Test : IDisposable { public ❔ How do I hide or show a div based on the value of a property of my selected option?When I select a habit type, that habit type has a Measurability property, and I want to show or hide❔ going from prefix commands to slash commands in discord.netSo the creator of this repo has archived the repo. Does anyone know, how can i easily convert to us❔ CodeTaskFactory problemLook at the image, ill try other projects later Also i've repair the vs install but nothing❔ How to convert a hex string to a Span<byte>?Is there an allocation free method to convert a hex string into the corresponding bytes without allo❔ Where can I find the implementation of System.Object.ReferenceEquals?I'd like to know how the reference comparison works under the hood but I've never looked up the sour✅ How can I make my code reusable?Hi! I asked a question before to calculate the average of 5 text boxes in one textbox. I wrote a co❔ Monogame or Godot?**Preface: **I want to create a large 2D open world game akin to Terraria/Starbound, I started in Un❔ Is there any way to fix the nord theme for c# on vs2022?As shown in the image the text is black and barely readable, was wondering if anyone had the same pr✅ C# CS0246``` using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst