Decimal types in C# are used for precise numeric representations with high precision and a wide range. They are suitable for financial calculations or any scenario requiring exact decimal representation.
Example:
decimal totalPrice = 99.95M;
Boolean Types:
Boolean types represent true or false values. They are commonly used for conditional statements and decision-making in programs.
Example:
bool isUserLoggedIn = true;
Integral Types:
Integral types are used for representing whole numbers. They include data types like int, long, and byte.
Example:
int numberOfItems = 10;
Floating Point Types:
Floating-point types, such as float and double, are used for representing numbers with a fractional part. They are suitable for scientific and engineering calculations.
Example:
double piValue = 3.14159265359;
Nullable Types:
Nullable types allow variables to have an additional value, null, in addition to their usual data type values. They are often used when a value might be missing or undefined.
Example:
int? nullableValue = null;
Rules for Defining Variables:
When defining variables in C#, you must follow these rules: Variable names must start with a letter or underscore (_), followed by letters, digits, or underscores. Variable names are case-sensitive. You cannot use C# reserved keywords as variable names. Variable names should be meaningful and follow a naming convention (e.g., camelCase or PascalCase).
Example:
int studentAge = 20; // Variable name follows camelCase convention.
short:
Represents a 16-bit signed integer.
Example:
short myShort = 42;
int:
Represents a 32-bit signed integer.
Example:
int myInt = 12345;
char:
Represents a 16-bit Unicode character.
Example:
char myChar = 'A';
float:
Represents a 32-bit floating-point number.
Example:
float myFloat = 3.14f;
double:
Represents a 64-bit floating-point number.
Example:
double myDouble = 2.71828;
String:
Represents a sequence of characters.
Example:
string myString = "Hello, World!";
Class:
A user-defined reference type used to create objects.
Example:
class MyClass
{
// Class members and methods go here
}
MyClass myObject = new MyClass();
Object:
The base type for all C# types; it can hold any value.
Example:
object myObject = 42;
Interface:
An interface defines a contract that a class must adhere to by implementing its methods and properties.
Example:
interface IShape
{
double CalculateArea();
}
Pointer Data Type:
Pointers in C# are typically used in unsafe contexts and are not commonly used in everyday C# programming due to safety concerns. They allow direct memory manipulation.
Example:
unsafe
{
int* ptr;
int num = 42;
ptr = #
Console.WriteLine(*ptr); // Prints the value 42
}