是否可以从可能的类型集中排除特定类型,可以在泛型参数中使用?如果是这样的话.
例如
Foo<T>() : where T != bool
Run Code Online (Sandbox Code Playgroud)
将意味着除bool类型之外的任何类型.
编辑
为什么?
以下代码是我尝试强制执行否定约束.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var x1=Lifted.Lift("A");
var x2=Lifted.Lift(true);
}
static class Lifted
{
// This one is to "exclude" the inferred type variant of the parameter
[Obsolete("The type bool can not be Lifted", true)]
static public object Lift(bool value) { throw new NotSupportedException(); }
// This one is to "exclude" the variant where …Run Code Online (Sandbox Code Playgroud) 我试过了
module Program
{
Main() : void
{ mutable x : byte = 0B;
mutable y : byte = 0B;
x++;
//y = x + 1;
//y = x + 1B;
//def one:byte=1;// x = x + one;
}
}
Run Code Online (Sandbox Code Playgroud)
无论我尝试哪一个,我都会收到以下错误消息.
错误1期望字节,在赋值中得到int:System.Int32不是System.Byte的子类型[simple require]
我发现作品的唯一方法就是
y = ( x + 1 ):>byte
Run Code Online (Sandbox Code Playgroud)
哪个有点faff,只是添加一个.
为什么是这样?还有更好的(阅读更短的方式)?
作为后续问题,基于类别的方法
父方法由其后代继承.
那么构造函数(例如New())是怎么回事?这对我来说似乎打破了继承.
是否有某个属性将其标记为特殊?(如果是这样的话是什么?)
可以这样请解释发生了什么.
想要在我的代码中编写的内容如下.
c² = a² + b²
Run Code Online (Sandbox Code Playgroud)
首先,我尝试创建一个宏².我尝试了以下内容.
macro @² (x)
syntax (x,"²")
{
<[
($x * $x)
]>
}
Run Code Online (Sandbox Code Playgroud)
但是我在期待一个标识符错误,(x)所以我尝试了
macro @s (x)
syntax (x,"²")
{
<[
($x * $x)
]>
}
Run Code Online (Sandbox Code Playgroud)
现在我得到了不支持的语法令牌错误"²".
所以我问1.可以编写运算符²吗?2.支持的语法标记是什么?
至于我可以告诉以下两个例子在功能方面是相同的.
C#
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
var x = new Example(12);
var y = new Example(34);
var z = Example.Examples.One;
}
}
class Example
{
public static class Examples
{
public static readonly Example Zero = new Example(0);
public static readonly Example One = new Example(1);
}
public readonly Double Value;
public Example(Double Value)
{
this.Value = Value;
}
public static Example Add(Example x, Example y)
{
return new Example(x.Value + y.Value);
} …Run Code Online (Sandbox Code Playgroud) Parallel.For中有这个错误吗?
public class DataPoint
{
public int Game { get; set; }
public byte Card { get; set; }
public byte Location { get; set; }
public DataPoint(int g,byte c,byte Loc)
{
Game = g;
Card = c;
Location = Loc;
}
public override string ToString()
{
return String.Format("{0} {1} {2}",Game,Card,Location);
}
}
Run Code Online (Sandbox Code Playgroud)
它必须是Parallel.For中的错误
private static System.Collections.Concurrent.ConcurrentBag<DataPoint> FillData()
{
var points = new System.Collections.Concurrent.ConcurrentBag<DataPoint>();
long c = 32768;
long z = 0;
Parallel.For(z, c, (i) =>
{
points.Add(new DataPoint( rand.Next(1, …Run Code Online (Sandbox Code Playgroud)