C#

Keywords, Identifiers & Data Types

0

Keywords

Keywords are reserved words in C# that have specific meanings. They’re pre-defined and used to instruct the compiler. Some examples:

  1. Reserved Keywords: Words with fixed roles like class, if, for, return, etc.

  2. Contextual Keywords: Special only in specific contexts, like get and set for properties.

Identifiers

An identifier is a name used to label a class, variable, function, or any other user-defined item in programming.

Rules for Naming Identifiers in C#:

  1. Must start with a letter, followed by letters, digits (0-9), or underscores.

  2. The first character cannot be a digit.

  3. No spaces or special symbols like ?, – +! @ # % ^ & * ( ) [ ] { } . ; : ” ‘ / and , except for underscore (_).

  4. Shouldn’t be a reserved C# keyword.

Identifiers give meaningful names to program elements, helping programmers understand and manage their code.

Data Types

The variables in C#, are categorized into the following types:

  • Value types

  • Reference types

  • Pointer types

1. Value Types:

Value types directly contain data and are assigned a value directly. They are derived from the System.ValueType class.
Examples include:

bool: Represents Boolean values (True or False).

byte: 8-bit unsigned integer (0 to 255).

char: 16-bit Unicode character (U+0000 to U+FFFF).

decimal: 128-bit precise decimal values with 28-29 significant digits.

double: 64-bit double-precision floating-point type.

float: 32-bit single-precision floating-point type.

int: 32-bit signed integer type.

long: 64-bit signed integer type.

sbyte: 8-bit signed integer type.

short: 16-bit signed integer type.

uint: 32-bit unsigned integer type.

ulong: 64-bit unsigned integer type.

ushort: 16-bit unsigned integer type.

2. Reference Types:

Reference types don’t contain actual data but reference memory locations. They allow multiple variables to refer to the same data. Changes to one variable automatically affect others. Examples include:

object: The base class for all types in C#.

dynamic: Allows storing any type, with type checking at runtime.

string: Used for string values.

3. Object Type (Boxing and Unboxing):

The object type is the base class for all data types. Values of any type can be assigned to object, but type conversion is needed.
When a value type is converted to object, it’s called boxing, and vice versa is called unboxing.

4. Dynamic Type
The dynamic type can store any value type or reference type, and type checking occurs at runtime. Syntax: dynamic variableName = value;.

5. String Type:

The string type is used for string values. It’s an alias for the System.String class and allows assigning string literals in both quoted and @quoted forms.

6. User-Defined Reference Types:

These are custom types defined by users, including classes, interfaces, and delegates. They allow creating complex data structures.

7. Pointer Types (Unsafe Codes):

Pointer types store memory addresses of other types. Pointers in C# provide similar capabilities as those in C or C++. Syntax: type* identifier;.

Leave a Reply

Your email address will not be published. Required fields are marked *