Naming conventions help make your code easy to read and understand by everyone.
Naming conventions in C#
Introduction
Syntax
C Sharp (C#)
PascalCase for classes, methods, and properties camelCase for local variables and method parameters ALL_CAPS with underscores for constants
PascalCase means each word starts with a capital letter, like MyClass.
camelCase means the first word starts lowercase, and next words start uppercase, like myVariable.
Examples
C Sharp (C#)
public class CustomerAccount { public int AccountNumber { get; set; } public void CalculateBalance() { } }
C Sharp (C#)
int itemCount = 5; string userName = "Alice";
C Sharp (C#)
const double PI = 3.14159;
Sample Program
This program shows naming conventions: constant in ALL_CAPS, local variable in camelCase, and class/method in PascalCase.
C Sharp (C#)
using System; public class Program { const int MAX_USERS = 100; public static void Main() { int currentUsers = 10; Console.WriteLine($"Max users allowed: {MAX_USERS}"); Console.WriteLine($"Current users: {currentUsers}"); } }
Important Notes
Follow these conventions to make your code easier to read and maintain.
Consistency is more important than the exact style chosen.
Use meaningful names that describe what the item represents.
Summary
Use PascalCase for classes, methods, and properties.
Use camelCase for local variables and method parameters.
Use ALL_CAPS for constants.
Practice
1. Which naming convention is correct for a C# class name?
easy
Solution
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.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).Final Answer:
MyClass -> Option AQuick 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
Solution
Step 1: Recall local variable naming rules
Local variables in C# use camelCase, starting with a lowercase letter and no underscores.Step 2: Match options to camelCase
localVariable uses camelCase correctly. LocalVariable uses PascalCase, LOCALVARIABLE uses all uppercase, and local_variable uses underscores.Final Answer:
localVariable -> Option AQuick 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
Solution
Step 1: Identify constant and variable values
MAX_COUNT is a constant with value 10, maxCount is a variable with value 5.Step 2: Understand Console.WriteLine outputs
Console.WriteLine(MAX_COUNT) prints 10, Console.WriteLine(maxCount) prints 5.Final Answer:
10 and 5 -> Option BQuick 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
Solution
Step 1: Check class naming convention
Class names must use PascalCase, but 'exampleClass' starts with lowercase.Step 2: Check property and field naming
Property 'MyProperty' uses PascalCase correctly; field 'myField' uses camelCase correctly.Final Answer:
Class name should be PascalCase -> Option CQuick 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
Solution
Step 1: Naming for constants
Constants use PascalCase, so MaxUsers is correct, max_users is incorrect.Step 2: Naming for method parameters
Method parameters use camelCase, so currentUserCount is correct, CurrentUserCount is incorrect.Final Answer:
MaxUsers and currentUserCount -> Option DQuick 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
10 questions ~5 mineasy hard
