-
Notifications
You must be signed in to change notification settings - Fork 2
/
CSharp.html
61 lines (56 loc) · 4.36 KB
/
CSharp.html
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
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<h4>Generics</h4>
<br />
<ul>
<li>design interfaces and generic classes that have the same implementation, regardless of the type of parameter, increasing the flexibility of use in a safe way </li>
<li> we apply constraints to generics using “where” keyword. Constraints will result in compile time error if you instantiate with placeholder that is not defined in constraints.</li>
</ul>
<table class="highlight tab-size js-file-line-container" data-tab-size="8" data-paste-markdown-skip="">
<tbody>
<tr>
<td id="file-generic-constraints-with-multiple-classname-cs-L1" class="blob-num js-line-number" data-line-number="1"></td>
<td id="file-generic-constraints-with-multiple-classname-cs-LC1" class="blob-code blob-code-inner js-file-line"><span class="pl-c"><span class="pl-c">//</span>Declaring multiple Generic with single & multiple class names using interface</span></td>
</tr>
<tr>
<td id="file-generic-constraints-with-multiple-classname-cs-L2" class="blob-num js-line-number" data-line-number="2"></td>
<td id="file-generic-constraints-with-multiple-classname-cs-LC2" class="blob-code blob-code-inner js-file-line"><span class="pl-k">public</span> <span class="pl-k">static</span> <span class="pl-k">void</span> <span class="pl-en">MultipleGenericExample1</span><<span class="pl-en">T1</span>, <span class="pl-en">T2</span>>(<span class="pl-en">T1</span> <span class="pl-smi">t1</span>, <span class="pl-en">T2</span> <span class="pl-smi">t2</span>) <span class="pl-k">where</span> <span class="pl-en">T1</span> : <span class="pl-en">ModelExample3</span>, <span class="pl-k">new</span>() <span class="pl-k">where</span> <span class="pl-en">T2</span> : <span class="pl-en">IModelExample</span></td>
</tr>
<tr>
<td id="file-generic-constraints-with-multiple-classname-cs-L3" class="blob-num js-line-number" data-line-number="3"></td>
<td id="file-generic-constraints-with-multiple-classname-cs-LC3" class="blob-code blob-code-inner js-file-line">{</td>
</tr>
<tr>
<td id="file-generic-constraints-with-multiple-classname-cs-L4" class="blob-num js-line-number" data-line-number="4"></td>
<td id="file-generic-constraints-with-multiple-classname-cs-LC4" class="blob-code blob-code-inner js-file-line"> <span class="pl-smi">Console</span>.<span class="pl-en">WriteLine</span>(<span class="pl-smi">String</span>.<span class="pl-en">Format</span>(<span class="pl-s"><span class="pl-pds">"</span>T1 Attributes {0} {1}<span class="pl-pds">"</span></span>, <span class="pl-smi">t1</span>.<span class="pl-smi">attribute1</span>, <span class="pl-smi">t1</span>.<span class="pl-smi">attribute2</span>));</td>
</tr>
<tr>
<td id="file-generic-constraints-with-multiple-classname-cs-L5" class="blob-num js-line-number" data-line-number="5"></td>
<td id="file-generic-constraints-with-multiple-classname-cs-LC5" class="blob-code blob-code-inner js-file-line"> <span class="pl-smi">Console</span>.<span class="pl-en">WriteLine</span>(<span class="pl-smi">String</span>.<span class="pl-en">Format</span>(<span class="pl-s"><span class="pl-pds">"</span>T2 Attributes {0} {1}<span class="pl-pds">"</span></span>, <span class="pl-smi">t2</span>.<span class="pl-smi">attribute1</span>, <span class="pl-smi">t2</span>.<span class="pl-smi">attribute2</span>)); </td>
</tr>
<tr>
<td id="file-generic-constraints-with-multiple-classname-cs-L6" class="blob-num js-line-number" data-line-number="6"></td>
<td id="file-generic-constraints-with-multiple-classname-cs-LC6" class="blob-code blob-code-inner js-file-line">}</td>
</tr>
</tbody>
</table>
static void Swap<T>
(ref T a,ref T b)
{
b = a;
a = b;
Console.WriteLine(b);
Console.WriteLine(a);
}
<br />
“default” key word
<br />
In generic types there is a default key word, it just resets the variable, the default value for int is zero.
<br />
</body>
</html>