Type inference
Learn how the compiler infers expression types and type compatibility.
camelCase and PascalCase
In C# naming decides how readable your code is. There are two common styles: camelCase and PascalCase.
In camelCase the first word starts lowercase, the rest are capitalized: firstName, totalPrice, isActive.
In PascalCase every word starts uppercase: FirstName, TotalPrice, CustomerOrder.
Local variables use camelCase; classes and methods use PascalCase. This is the common C# convention.
Keyword conflicts
C# has reserved words: class, int, string, return, if and many more. You cannot use them as variable names.
For example, int class = 5; is a compile error; class is a keyword.
Pick a meaningful suffix instead: write int classNumber = 5; or int classCount = 5;.
Keep names short but meaningful. Avoid vague names like x1, x2; prefer clear ones such as studentCount, orderTotal.
Exercise Declare three camelCase variables and print them
Assign 30 to int studentCount, "C#" to string courseName, and true to bool isOnline. Print the three variables on separate lines with Console.WriteLine.
Show example solution
First write int studentCount = 30; string courseName = "C#"; bool isOnline = true; then add three Console.WriteLine calls.
Example solution:
int studentCount = 30;
string courseName = "C#";
bool isOnline = true;
Console.WriteLine(studentCount);
Console.WriteLine(courseName);
Console.WriteLine(isOnline);
Test yourself
-
Which one is in camelCase?
- FirstName
- firstname
- First_Name
- firstName
Show answer
firstName — Correct! firstName is camelCase; first word lowercase, the rest uppercase.
-
Match each name to its style
- firstName
- camelCase example
- FirstName
- PascalCase example
- first name
- no spaces allowed
- 1user
- no leading digit
-
Write the PascalCase form
string ____ = "Hello"; // class name styleShow answer
string UserName = "Hello"; // class name style