Variables
Normal Declaration:
string msg;
msg = "Hello";
Implicit Type (var):
var msg = "Hello";
Constants
const double Phi = 1.618;
Strings
string str = "Hello";
Multiline string (verbatim)
string str = @"Multiline
string";
String interpolation
string name = "World";
string greeting = $"Hello, {name}!";
Numbers
整數 Integer
/// | `short`| 16-bit | -2¹⁵ ~ 2¹⁵-1 | -32,768 ~ 32,767 | 正負 3 萬,4 位數 |
/// | `int` | 32-bit | -2³¹ ~ 2³¹-1 | -2.1B ~ 2.1B | 正負 21 億,10 位數|
///| `long` | 64-bit | -2⁶³ ~ 2⁶³-1 | -9.2E ~ 9.2E | 正負 922 京,19 位數|
byte b = 255;
short s = 32000;
int num = 3;
long l = 100000L;
浮點 Floating
///| `float` | 32-bit | ±3.4×10³⁸ | ~7 位數 |
///| `double` | 64-bit | ±1.7×10³⁰⁸ | ~15 位數 |
///| `decimal` | 128-bit | ±7.9×10²⁸ | ~28 位數 |
float f = 3.14f;
double d = 3.14;
decimal m = 3.14m;
Arrays
// Fixed size array
int[] numbers = new int[5];
// Array with initializer
int[] numbers = { 1, 2, 3, 4, 5 };
// Multi-dimensional array
int[,] matrix = new int[3, 3];
Nullable Types
int? nullableInt = null;
nullableInt = 5;
// Null coalescing
int value = nullableInt ?? 0;
// Null conditional
string? name = null;
int? length = name?.Length;
Nullable types allow value types to represent undefined values.