The Conditional Attribute

Problem
You want to exclude a method and any statements that call it from a particular version of your application. How should you do it?

Solution
Prefer the Conditional attribute instead of the #if directive to exclude methods and all the statements that call them.

Comments
The Conditional attribute can abandon all statements that call a method, but it won’t discard the method definition itself. However, you can invoke the method via reflection.

Example

// This is incorrect.
#if BETAVERSION
    ShowSplashScreen();
#endif

void ShowSplashScreen() {
    ...
}

// This is correct. No need for an #if directive.
ShowSplashScreen();

[Conditional("BETAVERSION")]
void ShowSplashScreen() {
    ...
}

Rules for Success

Question
What are your top five rules for success?

Answer
1. Become self-reliant.
2. Be aggressive, attack, and operate on the offensive.
3. Cultivate grit and perseverance.
4. Develop into a person of value.
5. Never stop being a student.

Comments
In my opinion, the most important indicator of success in life is self-reliance. I’m with Robert Greene on this one. In life, the only constant is change. Depending on others, a job, a lifestyle, or anything for that matter, is just not reliable. As sad as it may be, you can only depend on yourself. So, I urge you to strive for bad-ass confidence. The next important value that I believe leads to success is aggression. No, I don’t mean being aggressive or belligerent to others. What I mean is it’s not enough to stand idly by waiting for the things you want. You need to attack and go get them. It’s better to let it all hang out than to be timid. What do you have to lose? In other words, don’t let things happen to you; instead, make them happen. Thank you, Dan Gable! Angela Duckworth’s noteworthy research revealed that grit and perseverance, in the long run, beat out talent. As a programmer and technologist, I personally take great comfort in this research. In general, however, you will constantly be challenged in life. Accept this fact and don’t give up easily. Simply put one foot in front of the other, take action, and grind. One of my favorite programmers, Sandi Metz, gave a great presentation on this. Albert Einstein said it best. “Try not to become a person of success but rather a person of value.” Strive to be useful at work and in your day-to-day life. Provide value, and the rewards will be aplenty. Finally, never stop learning. In order to be successful, you must have a basic understanding of what seems to be a limitless amount of topics. Continue to learn about your profession, but also learn about other important facets of daily life. For example, how a car works, how to prepare and file taxes, how to invest, and how to maintain your home. The list goes on.

The this Keyword

Question
What’s the best way to use the this keyword in C#?

Answer
Avoid using this to reference a field or property unless it makes the code clearer.

Comment
There is an exception, of course. Using this is okay in a method or constructor that contains a parameter or local variable whose name resembles a class-level field or property.

Example

class Car {
    // In a real-world app, these would be properties.
    public string Make;
    public string Model;

    public Car(string make, string model) {
        this.Make = make;
        this.Model = model;
    }
}

Non-CLS-Compliant Types in C#

Question
How do I handle public members that work with non-CLS (Common Language Specification) object types?

Answer
You want to avoid public methods or properties that take or return types that aren’t CLS-compliant. For example, unsigned integers. However, if you can’t avoid these members, mark them with the CLSCompliant(false) attribute.

Comments
The compiler only honors the CLSCompliant attribute for public members in public types. Additionally, a type or member can’t be marked as CLS-compliant unless the assembly is also attributed as CLSCompliant(true).

Example

public class SimpleClass {
    [CLSCompliant(false)]
    public void DoWork(uint someNumber) {
        ...
    }
}

Explicit Access Modifiers

Question
Should I omit access modifiers and accept the default when defining types and their members?

Answer
No, you’ll want to explicitly call out the scope for types and their members.

Comments
The default scope for type members in C# is private. Omitting access modifiers may confuse programmers coming to C# from other languages.

Example

// Not recommended.
void PerformTask() {
    ...
}

// This is preferred.
private void PerformTask() {
    ...
}

Scope for Members

Question
What’s the best way to apply scope to type members?

Solution
Use the internal access modifier when a member will not be invoked from other assemblies. Mark members private when they won’t be invoked by other types in the current assembly.

Comments
Don’t make fields, properties, or methods public unless necessary.

Nested Types

Question
What scope qualifier should I use for nested types?

Answer
For nested types, use a private or internal scope qualifier.

Comments
Nested types should only be used by their enclosing type. So, in most cases, there is no reason for making a nested type public. There is an exception. Nested comparers and enumerators are allowed public scope.

Language-Specific Keywords

Question
What’s the best way to specify the type of type members?

Answer
Use language aliases when specifying the type of fields, properties, and methods. For instance, use int instead of Int32 or System.Int32.

Comments
Using this convention is best because it’s more common and natural. Additionally, code editors render language-specific types in a different color. This improves code readability.

Examples

// Valid C#, but not ideal.
private Int32 age;

// The preferred method.
private int age;

// Alternative approach that includes a type suffix in the method name.
// Works, but not great since the type doesn't match the method name.
public int GetDataInt32()
{
    return ...
}

// Better.
public Int32 GetDataInt32()
{
    return ...
}