我爱Aspx >> C#.Net >> c#泛型学习(二)_C#教程95 }
96 public class MyCalculator : BaseCalculator<int>
97 {
98 public override int Add(int arg1, int arg2)
99 {
100 return arg1 + arg2;
101 }
102 //Rest of the methods
103 }
104
105 public interface ICalculator<T>
106 {
107 T Add(T arg1, T arg2);
108 //Rest of the methods
109 }
110 public class MyCalculator1 : ICalculator<int>
111 {
112 public int Add(int arg1, int arg2)
113 {
114 return arg1 + arg2;
115 }
116 //Rest of the methods
117 }
118 #endregion
119
120}
121
3.泛型方法
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace VS2005Demo2
6{
7
8 泛型方法#region 泛型方法
9 public class MyClass
10 {
11 public void MyMethod<T>(T t)
12 { }
13 }
14
15 public class Class3
16 {
17 public void Test()
18 {
19 MyClass obj = new MyClass();
20 obj.MyMethod<int>(3);
21
22 obj.MyMethod(3);
23 }
24 }
25 #endregion
26
27 编译器无法只根据返回值的类型推断出类型#region 编译器无法只根据返回值的类型推断出类型
28 public class MyClass1
29 {
30 public T MyMethod<T>()
31 { return default(T); }
32 }
33
34 public class Class31
Ҷƪл˵?
C# 3.0新特性初步研究 Part1:使用..[04-28]
C# 3.0新特性初步研究 Part2:使用..[04-28]
C# 3.0新特性初步研究 Part3:使用..[04-28]
C# 3.0新特性初步研究 Part4:使用..[04-28]
C# 3.0新特性初步研究 Part5:匿名..[04-28]
C# 3.0新特性初步研究 Part6:使用..[04-28]
C# 4.0语言将出现重大改变,带来..[04-28]
Word文档中快速插入分隔线的技巧..[04-28]
C# 2.0 套接字编程实例初探_C#教..[04-28]
DotNet(C#)学习-你学到什么程度..[04-28]
C# 3.0新特性初步研究 Part1:使用..[04-28]
C# 3.0新特性初步研究 Part2:使用..[04-28]
C# 3.0新特性初步研究 Part3:使用..[04-28]
C# 3.0新特性初步研究 Part4:使用..[04-28]
C# 3.0新特性初步研究 Part5:匿名..[04-28]
C# 3.0新特性初步研究 Part6:使用..[04-28]
C# 4.0语言将出现重大改变,带来..[04-28]
Word文档中快速插入分隔线的技巧..[04-28]
C# 2.0 套接字编程实例初探_C#教..[04-28]
DotNet(C#)学习-你学到什么程度..[04-28]