Types in C#
In .NET C#, types are fundamental building blocks that define and represent data. There are various types in .NET, and they can be broadly categorized into two main groups: value types and reference types. Here's a brief overview along with examples for each:
-
Value Types:
-
Integral Types:
-
int: Represents 32-bit signed integers.
int number = 42; -
long: Represents 64-bit signed integers.
long bigNumber = 1234567890123456789L; -
short: Represents 16-bit signed integers.
short smallNumber = 32767;
-
-
Floating-Point Types:
-
float: Represents 32-bit single-precision floating-point numbers.
float floatValue = 3.14f; -
double: Represents 64-bit double-precision floating-point numbers.
double doubleValue = 3.141592653589793;
-
-
Decimal Type:
- decimal: Represents 128-bit decimal numbers with higher precision and a smaller range.
decimal decimalValue = 123.45m;
- decimal: Represents 128-bit decimal numbers with higher precision and a smaller range.
-
Character Type:
- char: Represents a single 16-bit Unicode character.
char character = 'A';
- char: Represents a single 16-bit Unicode character.
-
Boolean Type:
- bool: Represents a Boolean value, either true or false.
bool isTrue = true;
- bool: Represents a Boolean value, either true or false.
-
Enumeration Types:
- enum: Represents a set of named integer constants.
enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }; Days today = Days.Wednesday;
- enum: Represents a set of named integer constants.
-
-
Reference Types:
-
Class Type:
- class: Represents a reference type that can contain data members, methods, properties, and events.
class Person { public string Name { get; set; } public int Age { get; set; } } Person person = new Person { Name = "John", Age = 30 };
- class: Represents a reference type that can contain data members, methods, properties, and events.
-
Interface Type:
- interface: Represents a contract for a class to implement.
interface ILogger { void Log(string message); } class ConsoleLogger : ILogger { public void Log(string message) { Console.WriteLine(message); } }
- interface: Represents a contract for a class to implement.
-
Array Type:
- array: Represents a collection of elements of the same type.
int[] numbers = { 1, 2, 3, 4, 5 };
- array: Represents a collection of elements of the same type.
-
Delegate Type:
- delegate: Represents a type that defines a method signature.
delegate int MathOperation(int x, int y); MathOperation add = (a, b) => a + b;
- delegate: Represents a type that defines a method signature.
-
String Type:
- string: Represents a sequence of characters.
string greeting = "Hello, World!";
- string: Represents a sequence of characters.
-
Nullable Type:
- Nullable<T> or T?: Allows value types to have a null value.
int? nullableNumber = null;
- Nullable<T> or T?: Allows value types to have a null value.
-
These are just a few examples, and .NET supports a wide range of types to cater to different programming needs.
No files yet, migration hasn't completed yet!