using SolrNet.Commands.Cores;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using Wrox4;
namespace Wrox7
{
//泛型接口
//在接口中可以定义的方法中带泛型参数
/*public interface IComparable<in T>
{
int CompareTo(T other);//.NET 1.0
}
public class Person : IComparable
{
private object lastname;
//在老版本中,需要带一个方法,强制转换换位特定的类型
public int CompareTo(object obj)
{
Person other = obj as Person;
return this.lastname.CompareTo(other.LastName);
}
public class Person2 : IComparable<Person>
{
private object LastName;
public int CompareTo(Person other)
{
LastName.CompareTo(other.LastName);
return LastName;
}
}
}
//协变和抗变指相对参数和返回值得类型进行转换
//参数类型是抗变的,举例说明
class Fex1
{
public void Display(Shape o)
{
//声明Display方法是为了接受Shape类型的对象作为其参数
var rv = new Rectangle { Width = 5, Height = 2.5 };
Display(rv);
}
public class Shape
{
public double Width { get; set; }
public double Height { get; set; }
public override string ToString() => $"Width:{Width},Height:{Height}";
}
public class Rectangle : Shape
{
}
}
//如果泛型类型用out关键字,泛型接口就是协变的,意味着返回类型只能是T
public interface IIndex<out T>
{
T this[int index] { get; }
int Count { get; }
}//接口用RectangleCollection类来实现,为泛型类型T定义了Rectangle
public class RectangleCollection : IIndex<Rectangle>
{
private Rectangle[] data = new Rectangle[3]
{
new Rectangle {Height = 2,Width = 5},
new Rectangle {Height = 3,Width = 6},
new Rectangle {Height = 4,Width = 7}
};
private static RectangleCollection _coll;
public static RectangleCollection GetRectangles() =>
_coll ??( _coll = new RectangleCollection());
public Rectangle this[int index]
{
get
{
if (index < 0 || index >= data.Length)
{
throw new ArgumentOutOfRangeException(nameof(index));
return data[index];
}
}
public int Count => data.Length;
}
}
//如果泛型类型用in关键字,泛型接口就是抗变的,这样接口只能把泛型类型T用作其方法的输入
class Fex2
{
public interface IDisplay <in T>
{
void Show(T item);
}
public class ShapeDisplay : IDisplay<Shape>
{
}
public static void Main()
{
}
}*/
//泛型结构 类似于泛型类只是没有继承特性
//Nullable<T>,C#这种数字不能为空,结构实现同值类型,唯一的系统开销是hasValue布尔字段,它是确定设置对应的值还是为空
class Program
{
public struct Nullable<T>
where T : struct
{
public Nullable(T value)
{
_hasValue = true;
_value = value;
}
private bool _hasValue;
public bool HasValue => _hasValue;
private T _value;
public T Value
{
get
{
if (!_hasValue)
{
throw new InvalidOperationException("no value");
}
return _value;
}
}
public static explicit operator T(Nullable<T> value)
{
return Value;
}
public static implicit operator Nullable<T>(T value) => new Nullable<T>(value);
public override string ToString() => !HasValue ? string.Empty: _value.ToString();
}
//Nullable<T>用Nullable<int>实例化,使用可空类型非常频繁,可以用定义可空变量,定义这类变量时,不能使用泛型结构的语法而使用?运算符
Nullable<int> x1;
int ? x2;
//int? x = GetNullType();
}
//泛型方法
class Program1
{
//除了可以定义泛型类之外,还可以定义泛型方法,在方法中,用方法声明定义
//泛型方法可以在非泛型类中定义
//Swap()方法把T定义为泛型类型,用于两个参数和一个变量temp
public void Swap<T>(ref T t1, ref T t2)
{
T temp;
temp = t1;
t1 = t2;
t2 = temp;
}//把泛型类型赋予方法调用,就可以调用泛型方法
static void Main()
{
int i = 4;
int j = 0;
//Swap<int>(ref i, ref j);
//Swap(ref i,ref j);
}
public class Account
{
public string Name { get; }
public decimal Price { get; }
public Account(string name, decimal price)
{
Name = name;
Price = price;
}
}
public static class Algorthms
{
public static decimal AccounteSimple(IEnumerable<Account> source)
{
decimal sum = 0;
foreach (Account a in source)
{
sum += a.Price;
}
return sum;
}
}
/*var accounts = new List<Account>()
{
new Account("Ch",1500),
new Account("St",2400)
};*/
//decimal amount = Algorthms.AccounteSimple(accounts);
//带约束的泛型方法
//泛型类可以用where子句限制,也可以用于泛型方法
/* public static decimal Accumulate<TAccount>(IEnumerable<TAccount> source)
where TAccount : IAccount
{
decimal sum = 0;
foreach(TAccount a in source)
{
//sum += a.Price;
}
}*/
}
}
C#基础-9
最新推荐文章于 2024-05-22 14:18:17 发布