Nullable Types

  • ู‡ูŠ ุนุจุงุฑุฉ ุนู† value types ุจุชุณู…ุญ ุจุงู„ null ูˆุจูŠูุถู„ ุฒูŠ ู…ุง ู‡ูˆ value type
  • ุจู†ุณุชุฎุฏู…ู‡ุง ูƒุชูŠุฑ ู…ุน ุงู„ ReadLine ู„ุงู† ู…ู…ูƒู† ุงู„ user ู…ูŠุฏุฎู„ุด ู‚ูŠู…ุฉ ูˆุงู„ู…ูุฑูˆุถ ุชุชุฎุฒู† Null
  • ูŠุนู†ูŠ ู…ุซู„ุง ุงู„int ู…ุด ุจูŠุณู…ุญ ุงู†ูƒ ุชุญุท null ูƒู‚ูŠู…ุฉ
int x = 50;
// x = null; // ERROR
 
int? Y; //Nullable int, IL (Nullable<int>)
Y = 5000;
Y = null;

ูƒู„ value type ู„ูŠู‡ุง ุงู„ nullable type ุจุชุงุนู‡ุง ูˆุงู„ reference types ูƒุฏุง ูƒุฏุง ุจุชุณู…ุญ ุจุงู„ null


Syntax Sugar

ู‡ูˆ ุนุจุงุฑุฉ ุนู† syntax sugar ู„ู€Cs Struct ุงุณู…ู‡ Nullable

int? x = 5; // Nullable<int>
x = null;
 
int? x = null;
Nullable<int> y = null;
Console.WriteLine(x);
Console.WriteLine(y);

ู…ููŠุด ููŠู‡ ุบูŠุฑ ู…ุชุบูŠุฑูŠู† ุงุชู†ูŠู† ุจุณ ูˆู‡ู…ุง HasValue ูˆ Value ูˆุทุฑูŠู‚ุฉ ุดุบู„ู‡ุง ุจุจุณุงุทุฉ ุฅู† ู„ูˆ ู‡ูˆ ุจ null ูŠุจู‚ุง ูƒุฏุง HasValue ูŠุจู‚ุง false ูˆู…ููŠุด ู‚ูŠู…ุฉ ููŠ ุงู„ู€ value ุฅู†ู…ุง ู„ูˆ true ูŠุจู‚ุง ูƒุฏุง ููŠู‡ ู‚ูŠู…ุฉ ููŠ ุงู„ู€ Value

ู„ูˆ ุฎุฏุช ุจุงู„ูƒ ุจุงู„ุทุฑูŠู‚ุฉ ุฏูŠ ู‡ูŠ ู‡ุชูุถู„ value type ู…ุด ู‡ุชุชุญูˆู„ ู„ู€ Reference ูˆู„ุง ุญุงุฌุฉ

Casting

ุทุจุนู‹ุง ุงุชุนู„ู…ู†ุง ุงุฒุงูŠ ู†ุนู…ู„ Cs Type Casting ูˆุงูŠู‡ ุงู„ู†ูˆุนูŠู† ุงู„ู„ูŠ ุนู†ุฏูŠ ูˆุงู„ูุฑู‚ ุจูŠู†ู‡ู…ุŒ ูุชุนุงู„ูˆ ู†ุทุจู‚ ู‡ู†ุง ู„ูˆ ุนุงูŠุฒ ุฃุนู…ู„ู‡ุง casting ุงูŠู‡ ุงู„ู…ุณู…ูˆุญ ูˆุงูŠู‡ ุงู„ู„ูŠ ู„ุง

int x = 50;
int? Y; //Nullable int
Y = null;
 
Y = X; // Safe Casting, Implicit (Small in Big)
X = Y; // UnSafe, Explicit: ERROR (Big in Small)
X = (int) Y;

Null Propagation (Conditional) Operator

ู…ุนู†ุงู‡ุง:

  • ู„ูˆ ุงู„ู„ูŠ ู‚ุจู„ ุงู„ู€ ? ุจู€ nullุŒ ู…ุชู†ูุฐุด ุงู„ู„ูŠ ุจุนุฏู‡ุง

ู…ุชุนู…ู„ุด call ู„ู„ length ู„ูˆ ุงู„ arr ุจ null

for (int i = 0; i < Arr?.Length; i++);

ู…ู…ูƒู† ุฃุณุชุฎุฏู…ู‡ุง ุฃูƒุชุฑ ู…ู† ู…ุฑุฉ ูˆุฑุง ุจุนุถ ุนุงุฏูŠ ุนุดุงู† ูƒุฏุง ุงุณู…ู‡ุง Propagation

class Dept
{
	public string Name;
}
 
class Employee
{
	public Dept Dept;
}
 
Empolyee E = default;
Console.WritLine(E.Dept.Name); // Unsafe
Console.WriteLine(E?.Dept?.Name??"Not Available");

ู…ุซุงู„ ุขุฎุฑ

int R = Arr.Length; //Unsafe
int? RR = Arr?.Length; //Safe, Nullable int 
//Arr?.Length === (Arr != null)? Arr.Length : null
int RRR = Arr?.Length??0; //Safe

Important Information

Default

ู„ูˆ ู…ุด ุนุงุฑู ุงุฏูŠ initial value ุงูŠู‡ ุจุฏูŠ ู„ู„ data type ูƒู„ู…ุฉ default

double D = default;
int [] Arr = default; // null, default value for all reference types

NullReferenceExeption

ู…ู† ุฃุตุนุจ ุงู„ู…ุดุงูƒู„ ุงู„ู„ูŠ ู…ู…ูƒู† ุชู‚ุงุจู„ ุฃูŠ programmer ู‡ูŠ ุงู„ NullReferenceExeption ุงู†ูŠ ุจุญุงูˆู„ ุฃูˆุตู„ ู„ุญุงุฌุฉ ุจุชุดุงูˆุฑ ุนู„ู‰ null

int [] Arr = default; // null
for (int i = 0; i < Arr.Length; i++)
	Console.WriteLine(Arr[i]);

ูุนุดุงู† ูƒุฏุง ู„ุงุฒู… ุชุนู…ู„ check ุนุดุงู† ุชูˆุตู„ ู„ู…ุฑุญู„ุฉ ุงู„ Cs Protective code

for (int i = 0; (Arr != null) && (i < Arr.Length); i++)

ุงู„ูุฑู‚ ุจูŠู† && , &

&&: ุจูŠุดูˆู ุงู„ุฃูˆู„ ู„ูˆ ู…ุญู‚ู‚ุด ุงู„ู…ุทู„ูˆุจ ุจูŠู‚ูู„ ุงู„ุจุฑู†ุงู…ุฌ ูŠุนู†ูŠ ููˆู‚ ู„ู‚ู‰ ุงู„ุฃูˆู„ ุจnull ูู…ุด ู‡ูŠูƒู…ู„ ุงู„ุชุงู†ูŠ

&: ุฏูŠ ุนุจุงุฑุฉ ุนู† bitwise ูˆู„ุงุฒู… ูŠุดูˆู ู‚ูŠู…ุฉ ุงู„ุงุชู†ูŠู† ูˆูŠุนู…ู„ู‡ู… anding ู…ุน ุจุนุถ