Hey everyone! I'm new to C# and diving into it with some simple projects. I'm wondering about when to use classes versus dictionaries for holding data. Coming from Python, I'm used to easily initializing a dictionary with key-value pairs, but in C# it seems a lot more complex. Is using a class the go-to option for more complex objects? I've also read that classes can be faster in execution.
On another note, I'm struggling with the explicit type declarations in C#. They seem much more complicated than they need to be for beginners! For instance, I found it tricky to figure out how to run a simple script in Visual Studio. How can I test snippets in isolation? Furthermore, I had a class in a different subfolder that wasn't recognized until I moved it. What's the point of having a namespace if the file still has to be in the same directory? I'd love to hear your thoughts on these topics!
1 Answer
Welcome to the C# world! You're right that classes and dictionaries can sometimes overlap in functionality, but they serve different purposes. Classes are great for creating complex data structures with methods and behaviors attached, while dictionaries are more straightforward key-value storage with optimized access times. For simple data collections or configurations, dictionaries can be handy, but for anything more intricate, you’ll benefit from using classes because they allow for encapsulation and easier maintenance.
Plus, when you get into object-oriented programming, classes become essential for organizing your code and reusing structures efficiently!

Thanks for the insight! So, if I’m transitioning complex logic from Python, sticking to classes in C# seems to be the best practice? I'm still grappling with how to initialize something like a dictionary in C# compared to Python.