gpt4 book ai didi

C# - 使用来自其他类的符号而不用类名限定

转载 作者:行者123 更新时间:2023-11-30 15:43:07 27 4
gpt4 key购买 nike

我在一个类中声明了一堆全局常量,现在想在其他类中使用这些常量,而不必总是在常量前面加上它们在其中声明的类的名称。示例:

namespace SomeName
{
public class Constants
{
public const int SomeConstant = 1;
}

public class SomeClass
{
void SomeFunc ()
{
int i = Constants.SomeConstant;
}
}
}

我想从 Constants.SomeConstant 中省略 Constants.。使用 import SomeName.Constants; 无效。有没有办法完成我想要的?如果是,我该怎么做?

最佳答案

不,你不可能那样做。

阅读了您的评论(“......以这种方式导入像 Math 这样的类会稍微缩短数学代码”)我可以推荐这个邪恶的代码:

class MathCalculations
{
private Func<double, double, double> min = Math.Min;
private Func<double, double, double> max = Math.Max;
private Func<double, double> sin = Math.Sin;
private Func<double, double> tanh = Math.Tanh;

void DoCalculations()
{
var r = min(max(sin(3), sin(5)), tanh(40));
}
}

关于C# - 使用来自其他类的符号而不用类名限定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7011423/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com