-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKod1_Nullable+boxing.cs
More file actions
32 lines (26 loc) · 999 Bytes
/
Kod1_Nullable+boxing.cs
File metadata and controls
32 lines (26 loc) · 999 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using System;
using System.Linq;
using System.Collections.Generic;
namespace HelloWorld;
/*Nullable + boxing
Напиши метод, который принимает object и возвращает int?: если внутри boxed int — верни его, если null — null, иначе null.C#int? UnboxToNullable(object value) { ... }Тесты:
UnboxToNullable(42) → 42
UnboxToNullable(null) → null
UnboxToNullable("text") → null
UnboxToNullable(new int?(100)) → 100 (boxing nullable)*/
public static class Program
{
static int? UnboxToNullable(object value)
{
if (value is int i)
return i;
return null;
}
public static void Main()
{
Console.WriteLine("Тест 1: " + UnboxToNullable(42));
Console.WriteLine("Тест 2: " + UnboxToNullable(null));
Console.WriteLine("Тест 3: " + UnboxToNullable("text"));
Console.WriteLine("Тест 4: " + UnboxToNullable(new int?(100)));
}
}