-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerics.cs
More file actions
171 lines (156 loc) · 4.53 KB
/
Generics.cs
File metadata and controls
171 lines (156 loc) · 4.53 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSharpProgramming
{
// STEP 1
//------------
/// <summary>
/// Generics-Class,Method,property,constructor,variable etc can be of generic type
/// </summary>
//internal class Generics
//{
// //How and Why Generics were introduced
// public static void Main()
// {
// bool result = Generics1.AreEqual(10,"Hello"); //value types
// if(result)
// {
// Console.WriteLine("EQUAL");
// }
// else
// {
// Console.WriteLine("NOT EQUAL");
// }
// }
//}
///// <summary>
///// LIMITATIONS-object-not type specific,poor performance-boxing and unboxing
///// </summary>
//public class Generics1
//{
// public static bool AreEqual(object a, object b)//reference type
// {
// //Console.WriteLine(a);
// //Console.WriteLine(b);
// //return a == b;
// return a.Equals(b);
// }
//}
//STEP 2
//------------------------
//Above limitation of the code is overcome by using the METHOD OVERLOADING
//LIMITATION IN METHOD OVERLOADING-REPEATATION OF CODE FOR THE SAME LOGIC
//internal class Generics
//{
// public static void Main()
// {
// // bool result = Generics1.AreEqual(10, 10);
// bool result1 = Generics1.AreEqual("Hello", "Hello");
// if (result1)
// {
// Console.WriteLine("EQUAL");
// }
// else
// {
// Console.WriteLine("NOT EQUAL");
// }
// }
//}
//public class Generics1
//{
// public static bool AreEqual(int a, int b)
// {
// return a.Equals(b);
// }
// public static bool AreEqual(string a, string b)
// {
// return a.Equals(b);
// }
//}
//STEP 3- TO OVERCOME THE LIMITATION OF METHOD OVERLOADING->GENERICS WERE INTRODUCED
//internal class Generics
//{
// public static void Main()
// {
// bool result = Generics1.AreEqual<int>(10, 10);
// bool result1 = Generics1.AreEqual<string>("Hello","Hello1");
// bool result2 = Generics1.AreEqual<double>(1.23,1.234);
// Generics1.Add<int, double>(10, 1.23);
// Generics1.Add<int, string>(10, "John");
// if (result2)
// {
// Console.WriteLine("EQUAL");
// }
// else
// {
// Console.WriteLine("NOT EQUAL");
// }
// }
//}
//public class Generics1
//{
// public static bool AreEqual<T>(T a,T b)
// {
// return a.Equals(b);
// }
// public static void Add<T,T1>(T a, T1 b)
// {
// Console.WriteLine($"Values are:{a} and {b}");
// }
//}
//Generics for various concepts in C#
public class Generics1<T>
{
/// <summary>
/// Generic variable
/// </summary>
private T ID;
/// <summary>
/// Generic Constructor
/// </summary>
/// <param name="id"></param>
public Generics1(T uid)
{
Console.WriteLine("USERID is:{0}",uid);
}
/// <summary>
/// Generic Method
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
public T Display(T msg)
{
Console.WriteLine("Employee id is:{0}",ID);
Console.WriteLine(typeof(T).ToString());
return msg;
}
/// <summary>
/// Generic Property
/// </summary>
public T EID
{
get { return ID; }
set { ID = value; }
}
}
public class Generics
{
public static void Main()
{
//Passing int as the type
Generics1<int> generics1 = new Generics1<int>(10);
generics1.EID = 100;
int res=generics1.Display(1);
Console.WriteLine("Message is:{0}", res);
Console.WriteLine("***********************************");
//Passing string as the type
Generics1<string> generics2 = new Generics1<string>("U10");
generics2.EID = "E100";
string res1 = generics2.Display("M1");
Console.WriteLine("Message is:{0}", res1);
}
}
}