Bird
Raised Fist0
C Sharp (C#)programming~10 mins

Naming conventions in C# - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Naming conventions in C#
Start Naming
↓
Identify Type
↓
Class
↓
Apply PascalCase
↓
Use Prefixes/Suffixes if needed
↓
Finish Naming
Choose the kind of item (class, method, variable), then apply the right naming style: PascalCase for classes and methods, camelCase for variables.
Execution Sample
C Sharp (C#)
class MyClass {
  void CalculateSum() {
    int totalAmount = 0;
  }
}
Defines a class, a method, and a variable using proper C# naming conventions.
Execution Table
StepCode ElementNaming Rule AppliedName ExampleReason
1ClassPascalCaseMyClassClasses use PascalCase to start with uppercase letters.
2MethodPascalCaseCalculateSumMethods use PascalCase to start with uppercase letters.
3VariablecamelCasetotalAmountVariables use camelCase to start with lowercase letters.
4Prefix/SuffixNone used-No prefixes or suffixes needed here.
5End--Naming complete following C# conventions.
💡 All code elements named following C# naming conventions.
Variable Tracker
VariableStartAfter Naming
ClassNameunnamedMyClass
MethodNameunnamedCalculateSum
VariableNameunnamedtotalAmount
Key Moments - 3 Insights
Why do classes and methods use PascalCase but variables use camelCase?
Classes and methods use PascalCase to stand out as types and actions, starting with uppercase letters (see execution_table rows 1 and 2). Variables use camelCase starting with lowercase to show they are data holders (row 3).
Can variable names start with uppercase letters?
By convention, variables start with lowercase letters (camelCase) to distinguish them from classes and methods. Using uppercase can confuse readers (see execution_table row 3).
When should prefixes or suffixes be used in names?
Prefixes or suffixes are used rarely, for example, 'I' prefix for interfaces or 'Async' suffix for async methods. In this example, none were needed (row 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what naming style is used for the method 'CalculateSum'?
APascalCase
Bsnake_case
CcamelCase
DUPPERCASE
💡 Hint
Check execution_table row 2 under 'Naming Rule Applied' and 'Name Example'.
At which step does the variable get its name following the camelCase convention?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at execution_table row 3 where the variable naming is described.
If we renamed the variable to 'TotalAmount' (starting uppercase), what would change in the variable_tracker?
AVariableName would change to 'TotalAmount' but still be camelCase
BVariableName would change to 'TotalAmount' and break camelCase convention
CClassName would change instead
DNo change would happen
💡 Hint
Refer to variable_tracker and key_moments about camelCase for variables.
Concept Snapshot
Naming conventions in C#:
- Classes and methods use PascalCase (Start with uppercase)
- Variables use camelCase (Start with lowercase)
- Prefixes/suffixes used for special cases (e.g., interfaces, async)
- Consistent naming improves code readability
- Follow these rules to write clear C# code
Full Transcript
In C#, naming conventions help keep code clear. Classes and methods use PascalCase, meaning their names start with uppercase letters, like MyClass or CalculateSum. Variables use camelCase, starting with lowercase letters, like totalAmount. Prefixes or suffixes are added only in special cases, such as 'I' for interfaces or 'Async' for asynchronous methods. Following these rules makes code easier to read and understand. The execution table shows each step naming the class, method, and variable correctly. The variable tracker shows how names change from unnamed to their final forms. Remember, variables should not start with uppercase letters to avoid confusion with classes or methods.

Practice

(1/5)
1. Which naming convention is correct for a C# class name?
easy
A. MyClass
B. myClass
C. my_class
D. MYCLASS

Solution

  1. Step 1: Understand class naming convention in C#

    Classes in C# use PascalCase, where each word starts with a capital letter and no underscores are used.
  2. Step 2: Compare options with PascalCase

    MyClass uses PascalCase correctly. my_class uses snake_case, myClass uses camelCase, and MYCLASS uses all uppercase (incorrect for classes).
  3. Final Answer:

    MyClass -> Option A
  4. Quick Check:

    Class names use PascalCase = MyClass [OK]
Hint: Classes always start with uppercase letters (PascalCase) [OK]
Common Mistakes:
  • Using camelCase for class names
  • Using underscores in class names
  • Using all uppercase for class names
2. Which of the following is the correct way to name a local variable in C#?
easy
A. localVariable
B. LocalVariable
C. LOCALVARIABLE
D. local_variable

Solution

  1. Step 1: Recall local variable naming rules

    Local variables in C# use camelCase, starting with a lowercase letter and no underscores.
  2. Step 2: Match options to camelCase

    localVariable uses camelCase correctly. LocalVariable uses PascalCase, LOCALVARIABLE uses all uppercase, and local_variable uses underscores.
  3. Final Answer:

    localVariable -> Option A
  4. Quick Check:

    Local variables use camelCase = localVariable [OK]
Hint: Local variables start lowercase, no underscores (camelCase) [OK]
Common Mistakes:
  • Starting local variables with uppercase letters
  • Using underscores in local variable names
  • Using all uppercase for local variables
3. What will be the output of the following code snippet?
const int MAX_COUNT = 10;
int maxCount = 5;
Console.WriteLine(MAX_COUNT);
Console.WriteLine(maxCount);
medium
A. Compilation error
B. 10 and 5
C. 5 and 10
D. maxCount and MAX_COUNT

Solution

  1. Step 1: Identify constant and variable values

    MAX_COUNT is a constant with value 10, maxCount is a variable with value 5.
  2. Step 2: Understand Console.WriteLine outputs

    Console.WriteLine(MAX_COUNT) prints 10, Console.WriteLine(maxCount) prints 5.
  3. Final Answer:

    10 and 5 -> Option B
  4. Quick Check:

    Constants print their value = 10, variables print their value = 5 [OK]
Hint: Console.WriteLine prints the values, not the names [OK]
Common Mistakes:
  • Confusing constant and variable values
  • Expecting names to print instead of values
  • Thinking naming affects output values
4. Identify the error in the following C# code snippet:
public class exampleClass {
    public int MyProperty { get; set; }
    private int myField;
}
medium
A. Field name should be PascalCase
B. Property name should be camelCase
C. Class name should be PascalCase
D. No errors, code is correct

Solution

  1. Step 1: Check class naming convention

    Class names must use PascalCase, but 'exampleClass' starts with lowercase.
  2. Step 2: Check property and field naming

    Property 'MyProperty' uses PascalCase correctly; field 'myField' uses camelCase correctly.
  3. Final Answer:

    Class name should be PascalCase -> Option C
  4. Quick Check:

    Class names = PascalCase, exampleClass is incorrect [OK]
Hint: Class names always start uppercase (PascalCase) [OK]
Common Mistakes:
  • Using lowercase for class names
  • Confusing property and field naming rules
  • Thinking camelCase is okay for classes
5. You have a constant for the maximum number of users and a method parameter for the current user count. Which naming is correct in C#?
public const int max_users = 100;
public void UpdateUserCount(int CurrentUserCount) { }
hard
A. MaxUsers and currentusercount
B. maxUsers and CurrentUserCount
C. max_users and current_user_count
D. MaxUsers and currentUserCount

Solution

  1. Step 1: Naming for constants

    Constants use PascalCase, so MaxUsers is correct, max_users is incorrect.
  2. Step 2: Naming for method parameters

    Method parameters use camelCase, so currentUserCount is correct, CurrentUserCount is incorrect.
  3. Final Answer:

    MaxUsers and currentUserCount -> Option D
  4. Quick Check:

    Constants PascalCase, parameters camelCase = MaxUsers, currentUserCount [OK]
Hint: Constants PascalCase, parameters camelCase (start lowercase) [OK]
Common Mistakes:
  • Using camelCase for constants
  • Starting parameters with uppercase letters
  • Using underscores in parameters
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard