✔ 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
Angius15mo 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
Graphics in 2013OP15mo ago
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
Angius15mo 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
Graphics in 2013OP15mo ago
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