我在MSDN上读到了一篇关于C#中泛型的优秀文章。
我脑海中浮现的问题是--为什么我要使用泛型约束?
例如,如果我使用如下代码:
public class MyClass<T> where T : ISomething
{
}
我不能用ISomething切换这个类中T的所有引用吗
使用这种方法的好处是什么?
我对为什么/什么时候想使用泛型方法感到有点困惑,因为一个非泛型方法可以访问其包含类的泛型成员,并被传递泛型参数。
所以,用一个可能忽略了重点的罐头例子(但强调了为什么我要问这个问题),我为什么要这样做:
public class SomeGeneric<T>
{
public T Swap<T>(ref T a, ref T b)
{
T tmp = a;
a = b;
b = tmp;
}
}
结束
public class SomeGeneric<T>
{
public T
我正在使用编写计算器,它能够在运行时计算OCaml表达式。
这是我的插件界面。
open Ocaml_plugin.Std
module type S = sig
val f : unit -> float
end
let univ_constr : (module S) Ocaml_dynloader.Univ_constr.t =
Ocaml_dynloader.Univ_constr.create ()
我可以使用签名unit -> float加载函数,例如,
let f () = 3.14159
let f () = 1.5 *. 1.5 *. 3.
并在主程序
可能重复:
考虑到以下代码:
import java.util.Collections;
import java.util.List;
public class ComeGetSome {
//TODO: private final Some<?> some = new Some();
private final Some some = new Some();
public static void main(String[] args) {
new ComeGetSome().dude();
}
public void dude() {
按照C#规范在10.4 Constants中使用
常量声明中指定的类型必须是s字节、字节、短、ushort、int、uint、long、ulong、char、float、double、十进制、bool、string、枚举类型、或引用类型。每个常量表达式必须产生目标类型的值或可以通过隐式转换转换为目标类型的类型的值(§6.1)。
为什么我不能做下面的事:
public class GenericClass<T>
where T : class
{
public const T val = null;
}
这应该是可能的,因为:
where T : clas
我有一个带有一个泛型参数的基本函数:
let func<'T> (x:'T when 'T : (static member op_Explicit: 'T -> float) ) =
float x
为什么我得到这样的错误:“声明的类型参数'T不能在这里使用,因为类型参数不能在编译时被解析”
在本例中,我提供了一个泛型参数,并将其约束为能够显式转换为float,而该函数所做的所有操作都转换为float,那么问题是什么呢?我已经阅读了所有与F#中的泛型相关的MSDN文档,但它们似乎只是原地踏步,与我在Visual Studio中看
这里有一个问题,第一个代码清单编译得很好(JDK 1.6 | JDK 1.7):
ArrayList<String> a = new ArrayList<String>();
String[] s = a.toArray(new String[0]);
但是,如果我将List引用声明为原始类型:
ArrayList a = new ArrayList();
String[] s = a.toArray(new String[0]);
我得到一个编译器错误,告诉我String[]是必需的,但是找到了Object[]。
这意味着我的编译器将泛型方法解释为返回Object[]
在VB.NET中,我可以声明如下函数:
Function Generic(Of T)(ByVal arg As String) As T
...
End Function
来自MSDN的文章:
不幸的是,我不能直接将这个函数C+P到一个VB6项目中。
VB6不支持'Of‘关键字吗?上面没有太多的文件。
,对于这个VB.NET示例是否有一个VB6等效的?
在Java中,这是有效的:
class Class1<T extends OtherType> { T t; ... }
//Inside other class that has no relation to Class1
private Class1 class1; //No type needed
public void someOtherFunction (Class1 class1) //Works
{
this.class1 = class1; //May warn about invalid type casting
class1.some
我有一个C#类定义如下:
public class GenericItem<T>
{
public List<T> Items { get; set; }
public DateTime TimeStamp { get; set; }
}
我正在我的服务器上创建这个类的实例。然后,我试图通过WCF服务通过线路传递它,如下所示:
[OperationContract]
public GenericItem<MyCustomType> GetResult()
{
GenericItem<MyCustomType> result = B
问题很简单。
我有三种类似的通用结构:
主结构
Public Structure Pointer(of T) 'Here is where the data type must be constrained
Dim BP As BasePointer
Dim BV As BaseValue(of T)
'Some code
End Structure
指针结构
Public Structure BasePointer 'This is a pointer itself, no problem (I think)
'Some co
抱歉,如果这是个愚蠢的问题。我一直在读那些快速的文档,我不知道泛型的意义。
如果我有以下情况:
func swapTwoInts<T>(inout a: T, inout b: T) {
let tempa = a
a = b
b = tempa
}
var num1 = "uno"
var num2 = "dos"
swap(&num1, &num2)
最近怎么样?
func swapTwoInts (inout a: Any, inout b: Any) {
let tempa = a
a = b
b = tempa
}
v
public abstract class EventSystemBase<T, K> : IAutoEvent
where T : Event
where K : IAutoEvent, new()
{
public static T Instance { get { return EventSystem.Events.OfType<T>().FirstOrDefault(); } }
public static IAutoEvent AutoEventInstance { get { return new K(); } }
我正在尝试测试我编写的一个java泛型类,这是我的测试。
public class BSTTest
{
public void testInsert()
{
int height;
BST<int> myTree = new BST<int>();
myTree.insert(1);
}
}
但当我编译时,我得到了意外类型的错误,它说如果找到一个整数,但需要在BST myTree = new BST()行上引用;这是什么意思?
下面是我的二进制搜索树和节点类,以供参考
public class
Im遵循一个泛型类的示例。因为我不想用测试代码填充项目的主要功能,所以我想创建一个静态演示函数,它运行代码示例。
我的代码:
namespace Syntax
{
public class GenericClass<T>
{
private class Node
{
private T data;
private Node next;
public Node(T t)
{
next = null;
我有一个数据对象,我到处传递:
public class TimedResult<T>
{
public ResultRequest Request { get; set; }
public T Data { get; set; }
public TimeSpan TimeTaken { get; set; }
}
在我的Manager类中,我做了一些引发事件的工作:
public void Execute(ResultRequest request)
{
var result = new TimedResult<DataTable>()
我正在尝试构建一个泛型类,它的构造函数引入了一个额外的类型,但是编译器拒绝。
我不太明白为什么以下内容不起作用:
public class Foo<T>
{
public Foo<T,TBar>(TBar tBar)
{
...
}
}
这并不重要,因为我可以使用fluent api编写类(这可能是首选的),但我仍然想了解为什么不行,我能想到的唯一解释是编译器不喜欢混合类级泛型类型声明的方法级泛型类型声明。
如果在Java中创建泛型类(类具有泛型类型参数),那么可以使用泛型方法(该方法采用泛型类型参数)吗?
请考虑以下示例:
public class MyClass {
public <K> K doSomething(K k){
return k;
}
}
public class MyGenericClass<T> {
public <K> K doSomething(K k){
return k;
}
public <K> List<K> makeSingletonList(K k){
为什么在第二个示例中没有编译错误,但是在第一个示例中有下面的编译错误?
SomeConcreateClass.java:1: ISomeBaseInterface cannot be inherited with different arguments: <java.lang.Object> and <>
public class SomeConcreateClass
^
1 error
在第二个示例中,惟一的差异类SomeDerivedClass不是参数化的。
Example1:
我是否正确地理解了在这个例子中
public class SomeConc
如果尝试使用[(.Explicit)]属性创建一个泛型结构,则使用该结构在运行时生成异常:
System.TypeLoadException:无法从程序集'bar‘加载类型'foo’,因为泛型类型不能具有显式布局。
我一直很难找到任何证据证明这种限制是存在的。文档强烈地暗示它是被允许和支持的。有人知道为什么这是不允许的吗?我想不出为什么泛型类型会降低它的可验证性。我觉得这是他们根本不想实现的边缘情况。
下面是关于为什么显式泛型布局有用的:
public struct TaggedUnion<T1,T2>
{
public TaggedUnion(T1
我正在尝试创建一个List<T>,其中用户通过从UI上的ComboBox中选择数据类型来指定数据类型。我已经设法创建了CustomEntity的一个对象,它包含List<T>,但是一旦我退出Button1_Click事件处理程序,CustomEntity就会超出作用域。可以创建一个类级变量吗?我试图注释掉它,因为它导致了一个错误。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
usi
试图创建具有泛型类型的静态字段并不编译:
class MyClass {
public static Function<Z, Z> blargh = new Function<Z, Z>() {
public Z apply(Z a) {
return a;
}
};
}
Eclipse说:
Multiple markers at this line
- Z cannot be resolved to a type
- Z cannot be resolved to a type
有人能解释一下,为什么我的接口中的一个简单的<T>在编译时破坏类型安全?见以下示例:
public class GenericsMain {
public static void main(String[] args){
A someA = new A() {
@Override
public List<String> listOfStrings() {
return Arrays.asList("A");
}
};
B someB = new B() {