相关疑难解决方法(0)

远程随机数,是这样的吗?

有人可以验证这种方法.我需要在两个长度范围内的长型号码.我使用返回int的.NET Random.Next(min,max)函数.如果我将long除以2,生成随机数并最终再乘以2,我的推理是否正确?或者我太热情......我明白我的随机解决方案会减少但是还有其他错误导致没有这样的随机数.

long min = st.MinimumTime.Ticks;    //long is Signed 64-bit integer
long max = st.MaximumTime.Ticks;
int minInt = (int) (min / 2);      //int is Signed 64-bit integer
int maxInt = (int) (max / 2);      //int is Signed 64-bit integer

Random random = new Random();
int randomInt = random.Next(minInt, maxInt);
long randomLong = (randomInt * 2);
Run Code Online (Sandbox Code Playgroud)

random c#-4.0

45
推荐指数
5
解决办法
5万
查看次数

在C#中等效C++的reinterpret_cast

我想知道reinterpret_castC#中的C++相当于什么!

这是我的样本:

class Base
{
    protected int counter = 0;
}

class Foo : Base
{
    public int Counter
    {
        get { return counter; }
    }
}

Base b = new Base();
Foo f = b as Foo; // f will be null
Run Code Online (Sandbox Code Playgroud)

我没有异议,f因为它应该是null.但如果它是C++我可以编写Foo f = reinterpret_cast<Foo>(b);并获得我想要的东西.我能做些什么来在C#中实现同样的目标?

PS.我假设Base并且Foo数据一致.

[UPDATE]

这是一个简单的场景,其中a reinterpret_cast可能会有所帮助:

考虑编写一个XXX-RPC库,您无法控制传入的参数,也无法控制要调用的服务的签名.您的库应该使用给定的参数调用所请求的服务.如果支持C#,reinterpret_cast我可以简单地reinterpret_cast将给定的参数放入预期的参数并调用服务.

c# reinterpret-cast

10
推荐指数
2
解决办法
7259
查看次数

标签 统计

c# ×1

c#-4.0 ×1

random ×1

reinterpret-cast ×1