-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
143 lines (115 loc) · 4.71 KB
/
Program.cs
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Collections.Generic;
namespace ConsumeTypes
{
class Program
{
static void Main(string[] args)
{
//Boxing and Unboxing Method:
BoxingAndUnboxing();
//Conversions and casting
Conversions();
//Using Dynamic Types: Office Automation API's
Console.WriteLine("Do you want to see how the Office Automation API for excel is used? Go check out the method declared in line 91(DisplayInExcel method).");
//Create Excel object(records)
//Add the following code to add reords to excel worksheet
// var entities = new List<dynamic>
// {
// new
// {
// ColumnA = 1,
// ColumnB = "Foo"
// },
// new
// {
// ColumnA= 2,
// ColumnB= "Bar"
// }
// };
// //Pass created object as parameter
// DisplayInExcel(entities);
}
//Boxing and Unboxing
//Boxing: Process of taking value type, placing it in new object on heap, storing ref to it on stack
//Unboxing: Takes item from heap, returns value that contains value from heap
public static void BoxingAndUnboxing()
{
//Boxing int value:
int i = 42;
object o = i;
Console.WriteLine("Boxed: {0}", o);
//Unboxing int value
int x = (int)o;
Console.WriteLine("Unboxed: {0}", x);
}
//Converting between different types
//4 Ways of conversion:
//1) Implicit conversions, 2) explicit conversions, 3) User-Defined conversions and 4) Conversion with helper class
public static void Conversions()
{
// 1) Implicit conversions: When conversion is legal, allowed
int i = 42;
double d = i;
Console.WriteLine("Implicit conversion(int to double): {0}", d);
// 2) Explicit conversions: when conversion is not allowed, needs to be casted
double d2 = 42.7;
int i2 = (int)d2;
Console.WriteLine("Explicit conversion(double to int): {0}", i2);
// 3) User-Defined conversions: When working with own types, you use both Impplicit- and Explicit conversions
var money = new Money(12.99M);
money.Amount = 12.99M;
// 4) Conversions with Helper class(BitConverter- and Converter class from System namespace)
//Convert class
int value1 = Convert.ToInt32("42");
Console.WriteLine("Convert.ToInt32 value: {0}", value1);
//Parse class
int value2 = int.Parse("42");
Console.WriteLine("int.Parse value: {0}", value2);
//TryParse(returns value and true/false)
bool success = int.TryParse("42", out int value3);
Console.WriteLine("Convertion succesfull: {0}, Value: {1}", success, value3);
}
//Dynamic Types: Excel worksheet
static void DisplayInExcel(IEnumerable<dynamic> entities)
{
var excelApp = new Microsoft.Office.Interop.Excel.Application();
excelApp.Visible = true;
excelApp.Workbooks.Add();
dynamic workSheet = excelApp.ActiveSheet;
workSheet.Cells[1, "A"] = "Header A";
workSheet.Cells[1, "B"] = "Header B";
var row = 1;
foreach (var entity in entities)
{
row++;
workSheet.Cells[row, "A"] = entity.ColumnA;
workSheet.Cells[row, "B"] = entity.ColumnB;
}
workSheet.Columns[1].AutoFit();
workSheet.Columns[2].AutoFit();
}
class Money
{
//Amount
public decimal Amount { get; set; }
//Parameterized constructor
public Money(decimal amount)
{
Amount = amount;
}
//Implicit operator
public static implicit operator decimal(Money money)
{
Console.WriteLine("Implicit operator: {0}", money.Amount);
return money.Amount;
}
//Explicit operator
public static explicit operator int(Money money)
{
Console.WriteLine("Explicit operator: {0}", (int)money.Amount);
return (int)money.Amount;
}
}
}
}