C# Fundamentals: Basic Syntax and Concepts

Keywords

Keywords are reserved words in C# that have special meaning and cannot be used as identifiers. They form the building blocks of the language syntax.

Common C# keywords include:

  • class: Defines a class
  • struct: Defines a structure
  • interface: Defines an interface
  • enum: Defines an enumeration
  • namespace: Defines a namespace
  • using: Imports namespaces
  • Access modifiers: public, private, protected, internal
  • static: Defines static members
  • readonly: Defines read-only fields
  • const: Defines constants
  • abstract: Defines abstract classes or members
  • virtual, override: Defines virtual and overridden methods
  • new: Hides members or specifies new implementations
  • base, this: References base class or current instance
  • as, is: For type casting and checking
  • typeof, nameof: Gets type or member information
  • Control flow: if, else, switch, case
  • Loops: for, foreach, while, do
  • Expection handling: try, catch, finally
  • Flow control: return, break, continue

Identifiers

Identifiers are names used to identify classes, variables, methods, and other user-defined items in C#. The rules for identifiers in C# are:

  • Must start with a letter (A-Z, a-z) or underscore (_)
  • Subsequent characters can be letters, digits (0-9), or underscores
  • Cannot contain spaces or special characters
  • Cannot be a C# keyword

Variables

Variables are storage locations with a name and a type. They hold data that can be modified during program exectuion.

Declaration syntax: DataType variableName;

// Declaration and initialization
int age = 25;
double height = 175.5;

// User input
Console.WriteLine("Enter a number:");
int userInput = int.Parse(Console.ReadLine());
Console.WriteLine("You entered: " + userInput);

Data Types

Numeric Types

C# supports various numeric types including integers and floating-point numbers.

Reference Types

Reference types store references to objects rather than the actual data. They include:

  • object: The base type for all types
  • dynamic: Type that is resolved at runtime
  • string: Sequence of characters
// Object type
object obj = "Hello";
object number = 42;

// Dynamic type
dynamic dyn = 10;
dyn = "Now I'm a string";

String Type

The string type represents text and is an alias for System.String.

Constants

Constants are variables whose values cannot be changed once initialized. They are declared using the const keyword.

const double PI = 3.14159;

Literals

Literals represent fixed values in code.

  • Integer literals: int x = 123;, int y = 0xFF;, int z = 0123;, int w = 0b1010;
  • Floating-point literals: float f = 3.14f;, double d = 3.14;
  • Character literals: char c = 'A';
  • String literals: string s = "Hello, world!";
  • Boolean literals: bool b1 = true;, bool b2 = false;
  • Null literal: object obj = null;
  • Decimal literal: decimal d = 123.45m;
  • DateTime literal: DateTime now = DateTime.Now;
  • Empty collections: int[] arr = new int[0];, List<int> list = new List<int>();</int></int>

Operators

Arithmetic Operators

Perform mathematical operations.

int a = 30, b = 20;
int sum = a + b;      // 50
int difference = a - b; // 10
int product = a * b;   // 600
int quotient = a / b;  // 1
int remainder = a % b; // 10
int postIncrement = a++; // 30 (then a becomes 31)
int preDecrement = --b;  // 19

Relational Operators

Compare values and return boolean results.

Logical Operators

Perform logical operations: && (AND), || (OR), ! (NOT).

Bitwise Operators

Operate on binary representations of data.

int a = 40;  // 0010 1000
int b = 13;  // 0000 1101
int and = a & b;    // 0000 1000 (8)
int or = a | b;     // 0010 1101 (45)
int xor = a ^ b;    // 0010 0101 (37)
int not = ~a;       // 1101 0111 (-41)
int leftShift = a << 2; // 1010 0000 (160)
int rightShift = b >> 2; // 0000 0011 (3)

Assignment Operators

Assign values to variables, with = being the most common.

Other Operators

  • sizeof: Gets size of a type in bytes
  • typeof: Gets the type of an object
  • ? :: Ternary conditional operator
  • is: Checks if an object is of a specific type
  • as: Performs safe type conversion
// Ternary operator
string grade = (score > 80) ? "A" : "B";

// Type checking
object obj = 42;
if (obj is int) Console.WriteLine("It's an integer");

// Safe casting
object strObj = "123";
string result = strObj as string;

Type Conversion

Implicit Conversion

Automatic conversion between compatible types with out data loss.

int small = 10;
long large = small; // Implicit conversion from int to long

Explicit Conversion

Conversion that might lose data and requires explicit casting.

double precise = 10.75;
int rounded = (int)precise; // Explicit conversion, truncates decimal part

Conversion Methods

  • Parse: Converts string to numeric types
  • Convert.ToXxx: Converts between various types
// Using Parse
string numberStr = "42";
int number = int.Parse(numberStr);

// Using Convert
string text = Convert.ToString(3.14);
int fromString = Convert.ToInt32("100");

Tags: C# programming Syntax Data Types Variables

Posted on Thu, 16 Jul 2026 16:19:12 +0000 by scottybwoy