C# – Deserialize JSON to a dictionary

I’ll show examples of how to deserialize JSON to dictionaries in different scenarios. Deserializing to Dictionary<string, string> When your JSON object has properties with simple string values (as opposed to nested objects), you can deserialize to Dictionary<string, string>. Here’s an example. Consider the following simple JSON object: Note: In all examples, I’ll show the pretty-printed … Read more

C# – ‘internal’ vs ‘protected’

The public/private access modifiers are straightforward: public means everything has access while private means only the class has access. The internal/protected access modifiers are a little more complicated. In other words, internal means it’s “private” to the assembly and protected means it’s “private” to the class and its subclasses. To illustrate the difference, I’ll show … Read more

C# – Default access modifiers

Classes (and other types) are internal by default. Class members (methods/properties/fields) are private by default. These defaults are applied when you don’t explicitly declare an access modifier. Here’s an example: Since the access modifiers aren’t declared, it uses the defaults. The class is internal while all of the members are private. This is almost never … Read more

Primitive Obsession code smell

The primitive obsession code smell means you’re using primitive types (ex: string, int) excessively instead of encapsulating them in objects. This leads to sloppy code that’s error prone, such as when you have very long parameter lists full of primitives. I’ll show an example of this problem and how to fix it. Here’s a simple … Read more

Refactoring the Large Class code smell

The Large Class code smells refers to a class that has too many responsibilities. It’s doing too much. Ideally a class should only have one responsibility (Single Responsibility Principle). Code Smell: Large Class Definition: A class has too many responsibilities. Solution: Large Class code smell example Here’s an example of the Large Class code smell … Read more