امکان تعریف اعضای static abstract در اینترفیسهای C# 11
نویسنده: وحید نصیری
تاریخ: ۱۴۰۱/۰۸/۲۷ ۱۱:۳۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
namespace CS11Tests;
public class StaticAbstractMembers
{
public static void Test()
{
var sum = AddAll(new[] { 1, 2, 3, 4 });
Console.WriteLine(sum);
}
private static int AddAll(int[] values)
{
int result = 0;
foreach (var value in values)
{
result += value;
}
return result;
}
} var sum = AddAll(new[] { 1, 2, 3, 4, 0.68 }); Argument 1: cannot convert from 'double[]' to 'int[]' [CS11Tests]csharp(CS1503)
private static T AddAll<T>(T[] values)
{
T result = 0;
foreach (var value in values)
{
result += value;
}
return result;
} T result = T.Zero;
using System.Numerics;
namespace CS11Tests;
public class StaticAbstractMembers
{
public static void Test()
{
//var sum = AddAll(new[] { 1, 2, 3, 4 });
var sum = AddAll(new[] { 1, 2, 3, 4, 0.68 });
Console.WriteLine(sum);
}
private static T AddAll<T>(T[] values) where T : INumber<T>
{
T result = T.Zero;
foreach (var value in values)
{
result += value;
}
return result;
}
} abstract static TSelf One { get; }
abstract static TSelf Zero { get; } abstract static TResult operator +(TSelf left, TOther right);
public interface ISport
{
bool IsTeamSport();
}
public class Swimming : ISport
{
public bool IsTeamSport() => false;
}
public class Football : ISport
{
public bool IsTeamSport() => true;
} public class StaticAbstractMembers
{
public static void Display<T>(T sport) where T : ISport
{
Console.WriteLine("Is Team Sport:" + sport.IsTeamSport());
}
} Display(new Football());
public interface ISport
{
static abstract bool IsTeamSport();
}
public class Swimming : ISport
{
public static bool IsTeamSport() => false;
}
public class Football : ISport
{
public static bool IsTeamSport() => true;
} public class StaticAbstractMembers
{
public static void Display<T>() where T : ISport
{
Console.WriteLine("Is Team Sport:" + T.IsTeamSport());
}
} public static T ParseIt<T>(string content, IFormatProvider? provider) where T : IParsable<T>
{
return T.Parse(content, provider);
}
public IEnumerable<T> ParseCsvRow<T>(string content, IFormatProvider? provider) where T : IParsable<T>
{
return content.Split(',').Select(str => T.Parse(str, provider));
} public static T ParseIt<T>(string content, IFormatProvider? provider)
{
var type = typeof(T);
var method = type.GetMethod("Parse", BindingFlags.Static | BindingFlags.Public,
new[] { typeof(string), typeof(IFormatProvider) });
return (T)method!.Invoke(null, new object?[] { content, provider })!;
} static T Add<T>(T left, T right) where T : INumber<T> => left + right;
public readonly struct Int32 : IComparable, IComparable<int>, IConvertible, IEquatable<int>, IFormattable, IParsable<int>, ISpanFormattable, ISpanParsable<int>, IAdditionOperators<int, int, int>, IAdditiveIdentity<int, int>, IBinaryInteger<int>, IBinaryNumber<int>, IBitwiseOperators<int, int, int>, IComparisonOperators<int, int, bool>, IEqualityOperators<int, int, bool>, IDecrementOperators<int>, IDivisionOperators<int, int, int>, IIncrementOperators<int>, IModulusOperators<int, int, int>, IMultiplicativeIdentity<int, int>, IMultiplyOperators<int, int, int>, INumber<int>, INumberBase<int>, ISubtractionOperators<int, int, int>, IUnaryNegationOperators<int, int>, IUnaryPlusOperators<int, int>, IShiftOperators<int, int, int>, IMinMaxValue<int>, ISignedNumber<int>
public interface INumber<TSelf> : IComparable, IComparable<TSelf>, IEquatable<TSelf>, IFormattable, IParsable<TSelf>, ISpanFormattable, ISpanParsable<TSelf>, IAdditionOperators<TSelf, TSelf, TSelf>, IAdditiveIdentity<TSelf, TSelf>, IComparisonOperators<TSelf, TSelf, bool>, IEqualityOperators<TSelf, TSelf, bool>, IDecrementOperators<TSelf>, IDivisionOperators<TSelf, TSelf, TSelf>, IIncrementOperators<TSelf>, IModulusOperators<TSelf, TSelf, TSelf>, IMultiplicativeIdentity<TSelf, TSelf>, IMultiplyOperators<TSelf, TSelf, TSelf>, INumberBase<TSelf>, ISubtractionOperators<TSelf, TSelf, TSelf>, IUnaryNegationOperators<TSelf, TSelf>, IUnaryPlusOperators<TSelf, TSelf> where TSelf : INumber<TSelf>?