首页 / C# / c# – 如何使用泛型变量执行数学运算?
c# – 如何使用泛型变量执行数学运算?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 如何使用泛型变量执行数学运算?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4408字,纯文字阅读大概需要7分钟。
内容图文
![c# – 如何使用泛型变量执行数学运算?](/upload/InfoBanner/zyjiaocheng/795/1571a3e43b994edd8498eabc109af3c4.jpg)
我正在开发一个需要整数字符串表示的项目,但是使用.NET Framework本身不支持的奇数数字库(据我所知) – 例如base36,62,64等.
我们决定编写一个可以与任何数字库一起使用的整体字符串转换系统,因为它足够简单.
稍后,我们想要创建一个自定义的IFormatProvider / ICustomFormatter,以便在未来使用时更容易使用.
但首先,我们希望以一些静态方法的形式解决转换过程本身,这些方法执行转换并返回一些基本结果.
一旦我们开始工作,我们就会把IFormatProvider封装器放到位.
自从我使用C#泛型以来已经有一段时间了,我不记得如何使编译器对此操作感到满意.
我更喜欢创建一个私有静态通用ConvertInteger方法,然后我可以从公共静态转换方法调用它来帮助强制执行强类型而不会使它变得混乱.
此设置的一个主要原因是在转换有符号值与无符号值时避免符号位出现问题.
现在我们有long和ulong的公共静态转换方法,以帮助避免有符号和无符号值之间的转换问题.
在将来,假设我们可以使用私有静态泛型方法,我们希望将公共方法扩展为包括int,uint,short,ushort,byte和sbyte作为显式实现,以帮助执行此方法时的性能大批量不同整数值的方法.
这是通用方法设计将派上用场的地方,因此我们不必一遍又一遍地复制完全相同的代码(使测试和调试也变得更加简单).
我遇到的问题是,编译器不允许我使用泛型类型执行比较或数学运算,因为它不知道如何在提供泛型类型参数值之前执行操作.
(作为主要的C开发人员,这真的让我感到烦恼,因为C通过简单地避免尝试解释通用代码直到提供了类型参数值来解决这个问题.)
使用这种通用方法设计,我需要做些什么来满足编译器?
我已经提供了下面的代码,注释表明特定的编译错误.
public static class NumericStringConverter
{
private static readonly string[] StandardDigits = new string[]
{
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
"_", "-"
};
private static string ConvertInteger<T>(T Value, T Base)
{
if (Base < 2) // error here: "Operator '<' cannot be applied to operands of type 'T' and 'int'"
throw new ArgumentOutOfRangeException("Base", Base, "The NumericStringConverter.Convert(Value, Base) method was called, with the Base parameter set to a value less than 2.");
if (Base > 64) // error here: "Operator '>' cannot be applied to operands of type 'T' and 'int'"
throw new ArgumentOutOfRangeException("Base", Base, "The NumericStringConverter.Convert(Value, Base) method was called, with the Base parameter set to a value greater than 64.");
if (Value == 0) // error here: "Operator '==' cannot be applied to operands of type 'T' and 'int'"
return StandardDigits[0];
string strResult = "";
bool IsValueNegative = (Value < 0); // error here: "Operator '<' cannot be applied to operands of type 'T' and 'int'"
while (Value != 0) // error here: "Operator '!=' cannot be applied to operands of type 'T' and 'int'"
{
strResult = strResult.Insert(0, StandardDigits[Math.Abs(Value % Base)]); // error here: "Operator '%' cannot be applied to operands of type 'T' and 'T'"
Value /= Base; // error here: "Operator '/=' cannot be applied to operands of type 'T' and 'T'"
}
if (IsValueNegative)
strResult = strResult.Insert(0, "-");
return strResult;
}
public static string Convert(long Value, long Base)
{
return ConvertInteger<long>(Value, Base);
}
public static string Convert(ulong Value, ulong Base)
{
return ConvertInteger<ulong>(Value, Base);
}
public static T Convert<T>(string Value, T Base)
{
return default(T); // TODO: convert a string back into an integer value.
}
}
解决方法:
既然你不能定义只允许整数类型的泛型约束,我只会有一个适用于long的方法和int的重载:
private static string ConvertInteger(long Value, byte Base)
{
if (Base < 2)
throw new ArgumentOutOfRangeException("Base", Base, "The NumericStringConverter.Convert(Value, Base) method was called, with the Base parameter set to a value less than 2.");
if (Base > 64)
throw new ArgumentOutOfRangeException("Base", Base, "The NumericStringConverter.Convert(Value, Base) method was called, with the Base parameter set to a value greater than 64.");
if (Value == 0)
return StandardDigits[0];
string strResult = "";
bool IsValueNegative = (Value < 0);
while (Value != 0)
{
strResult = strResult.Insert(0, StandardDigits[Math.Abs(Value % Base)]);
Value /= Base;
}
if (IsValueNegative)
strResult = strResult.Insert(0, "-");
return strResult;
}
public static string Convert(int Value, byte Base)
{
return ConvertInteger((long)Value, Base);
}
public static string Convert(ulong Value, byte Base)
{
return ConvertInteger((long)Value, Base); // check for overflow?
}
内容总结
以上是互联网集市为您收集整理的c# – 如何使用泛型变量执行数学运算?全部内容,希望文章能够帮你解决c# – 如何使用泛型变量执行数学运算?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。