C# Basic Syntax
This chapter will detail the basic syntax of C#, including variables, data types, operators, constants, and other core concepts, laying a solid foundation for subsequent learning.
Variables and Identifiers
Variable Declaration
In C#, variables must be declared before use:
Variable Initialization
Identifier Naming Rules
Naming Conventions:
- Camel Case:
firstName,lastName(for local variables and parameters) - Pascal Case:
FirstName,LastName(for classes, methods, properties) - Underscore Prefix:
_privateField(for private fields) - Constants:
MAX_VALUE,PI(all uppercase)
Data Types
Value Types
Integer Types
Floating Point Types
Other Value Types
Reference Types
String Type
Arrays
Object Type
Type Conversion
Implicit Conversion
Explicit Conversion
Constants
const Keyword
readonly Keyword
Operators
Arithmetic Operators
Comparison Operators
Logical Operators
Bitwise Operators
Comments
Single Line Comments
Multi-line Comments
XML Documentation Comments
Nullable Types
Nullable Value Types
Nullable Reference Types (C# 8.0+)
Summary
In this chapter, you learned:
- Variable declaration and initialization
- Identifier naming rules and conventions
- C# data types (value types and reference types)
- Type conversion (implicit and explicit)
- Constants (const and readonly)
- Operators (arithmetic, comparison, logical, bitwise)
- Comments and documentation
- Nullable types
These fundamental concepts form the foundation of C# programming. In the next chapter, we'll explore control structures to add logic and flow control to your programs.