题目 1: 泛型类
编写一个泛型类 Box,它能够存储一个类型为 T 的值,并提供方法 SetItem 和 GetItem 来设置和获取该值并给出使用示例。
public class Program
{
public class Box<T>
{
private T _item;
public void SetItem(T item)
{
_item= item;
}
public T GetItem()
{
return _item;
}
}
static void Main(string[] args)
{
Box<int> intbox = new Box<int>();
intbox.SetItem(123);
Console.WriteLine(intbox.GetItem());
Box<string> stringbox = new Box<string>();
stringbox.SetItem("Hello");
Console.WriteLine(stringbox.GetItem());
}
}
题目 2: 泛型方法
编写一个泛型方法 Swap,它接受一个数组和两个索引,交换数组中这两个位置的元素,并给出使用示例。
public static void Swap<T>(T[] array,int index1,int index2)
{
T temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
}
static void Main(string[] args)
{
int[] numbers = { 1, 2, 3, 4, 5, 6 };
foreach(var number in numbers)
{
Console.Write(number);
}
Swap(numbers, 0, 5);
Console.WriteLine();
foreach(var number in numbers)
{
Console.Write(number);
}
}
题目 3: 泛型接口
定义一个泛型接口 IRepository,它包含两个方法:Add 和 Get。Add 方法用于添加类型 T 的对象,Get 方法用于获取类型 T 的对象,并给出使用示例。
public class Program
{
public interface IRepository<T>
{
public void Add(T value);
public T Get();
}
public class SampleClass:IRepository<int>
{
private int t;
public void Add(int value)
{
t=value;
}
public int Get()
{
return t;
}
}
static void Main(string[] args)
{
IRepository<int> repository = new SampleClass();
repository.Add(1);
Console.WriteLine(repository.Get());
}
}
题目 4: 泛型约束
编写一个泛型方法 PrintTypeName,它接受一个泛型参数 T 并打印 T 的类型名称。要求使用泛型约束来确保 T 是一个引用类型,并给出使用示例。
public class Person
{
public int Age { get; set; }
public string Name { get; set; }
}
public class Program
{
public static void PrintTypeName<T>(T t)where T : class
{
Console.WriteLine($"Type Name:{typeof(T).Name}");
}
static void Main(string[] args)
{
PrintTypeName("Hello,world");
PrintTypeName(new Person());
}
}
题目 5: 泛型集合
创建一个泛型列表 MyList,它支持添加和移除元素,并且可以遍历列表中的元素,并给出使用示例。
public class Program
{
public class MyList<T>
{
public List<T> list=new List<T>();
public void Add(T item)
{
list.Add(item);
}
public void RemoveAt(int item)
{
list.RemoveAt(item);
}
public void Foreach()
{
foreach(var item in list)
{
Console.WriteLine(item);
}
}
}
static void Main(string[] args)
{
MyList<int>mylist=new MyList<int>();
mylist.Add(1);
mylist.Add(2);
mylist.Add(3);
mylist.Foreach();
mylist.RemoveAt(0);
mylist.Foreach();
}
}
题目6:
编写一个拓展方法 Filter,扩展 IEnumerable,接收一个 Func<T, bool> 作为参数,并返回一个仅包含符合条件的元素的集合。要求实现一个简单的筛选功能,比如筛选出集合中的所有偶数。
public static class IEnumerableExtensions
{
public static IEnumerable<T> Filter<T>(this IEnumerable<T> source, Func<T, bool> predicate)
{
foreach(T item in source)
{
if (predicate(item))
{
yield return item;
}
}
}
}
class Program
{
static void Main(string[] args)
{
IEnumerable<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6 };
IEnumerable<int> evenNumbers = numbers.Filter(n => n % 2 == 0);
foreach(int n in evenNumbers)
{
Console.WriteLine(n);
}
}
}
题目7:
创建一个包含事件 OnValueChanged 的类 Notifier。定义一个委托 ValueChangedHandler,用于处理 OnValueChanged 事件。然后在主程序中,创建 Notifier 实例并订阅事件,当事件被触发时打印一条消息。
{
public delegate void ValueChanedHandler(Object sender,EventArgs args);
public class Notifier
{
public event ValueChanedHandler OnValueChaned;
protected virtual void OnValueChangedEvent()
{
OnValueChaned?.Invoke(this, EventArgs.Empty);
}
public void TriggerValueChaned()
{
OnValueChangedEvent();
}
}
class Program
{
static void Main(string[] args)
{
Notifier notifier= new Notifier();
notifier.OnValueChaned += Notifier_OnValueChanged;
notifier.TriggerValueChaned();
Console.ReadKey();
}
private static void Notifier_OnValueChanged(object sender, EventArgs args)
{
Console.WriteLine("Value changed event triggered.");
}
}
}
题目8:
定义一个委托 Printer,可以处理输出字符串的方法。创建两个方法 PrintUpperCase 和 PrintLowerCase,分别将字符串转换为大写和小写。然后,创建一个多播委托,先调用 PrintUpperCase,再调用 PrintLowerCase。
public delegate void Printer(string message);
class Program
{
public static void PrintUpperCase(string message)
{
Console.WriteLine(message.ToUpper());
}
public static void PrintLowerCase(string message)
{
Console.WriteLine(message.ToLower());
}
static void Main(string[] args)
{
Printer printUpperCaseDelegate = PrintUpperCase;
Printer printLowerCaseDelegate= PrintLowerCase;
Printer multiCaseDelegate = printUpperCaseDelegate + printLowerCaseDelegate;
multiCaseDelegate("Hello World!");
}
}
题目9:
编写一个方法 PerformOperation,接受一个 Func<int, int, int> 类型的委托作为参数,该委托执行两个整数的操作。调用 PerformOperation 方法并传入一个委托,用于计算两个整数的差值。
class Program
{
static void Main(string[] args)
{
PerformOperation((x, y) => x - y, 10, 5);
}
static void PerformOperation(Func<int,int,int> operation,int num1,int num2)
{
int result= operation(num1,num2);
Console.WriteLine(result);
}
}