我正在做一些关于收益率回归性能的测试,我发现它比正常回报慢.
我测试了值变量(int,double等)和一些引用类型(字符串等)......并且两种情况下的yield return都较慢.为什么要用呢?
看看我的例子:
public class YieldReturnTeste
{
private static IEnumerable<string> YieldReturnTest(int limite)
{
for (int i = 0; i < limite; i++)
{
yield return i.ToString();
}
}
private static IEnumerable<string> NormalReturnTest(int limite)
{
List<string> listaInteiros = new List<string>();
for (int i = 0; i < limite; i++)
{
listaInteiros.Add(i.ToString());
}
return listaInteiros;
}
public static void executaTeste()
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
List<string> minhaListaYield = YieldReturnTest(2000000).ToList();
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, …Run Code Online (Sandbox Code Playgroud) 检查下面的代码:
class Money
{
public Money(decimal amount)
{
Amount = amount;
}
public decimal Amount { get; set; }
public static implicit operator decimal(Money money)
{
return money.Amount;
}
public static explicit operator int(Money money)
{
return (int)money.Amount;
}
}
Run Code Online (Sandbox Code Playgroud)
我不明白它在我的代码中会有用,我不能只做一个像这样的方法:
public static int returnIntValueFrom(Money money)
{
return (int)money.Amount;
}
Run Code Online (Sandbox Code Playgroud)
实施不是更容易和更清楚吗?
我正在编写一个生成DataTable的方法,将数据源作为通用的IEnumerable.如果没有值,我试图在字段上设置默认值,代码如下:
private void createTable<T>(IEnumerable<T> MyCollection, DataTable tabela)
{
Type tipo = typeof(T);
foreach (var item in tipo.GetFields() )
{
tabela.Columns.Add(new DataColumn(item.Name, item.FieldType));
}
foreach (Pessoa recordOnEnumerable in ListaPessoa.listaPessoas)
{
DataRow linha = tabela.NewRow();
foreach (FieldInfo itemField in tipo.GetFields())
{
Type typeAux = itemField.GetType();
linha[itemField.Name] =
itemField.GetValue(recordOnEnumerable) ?? default(typeAux);
}
}
}
Run Code Online (Sandbox Code Playgroud)
它抛出了这个错误:
找不到类型或命名空间名称'typeAux'(您是否缺少using指令或程序集引用?)
为什么?"Default(Type)"函数不应该返回该类型的默认值吗?
我有一个问题,我从一些数据库图像中收到大量直方图。这些直方图表示为向量 (0...255),我必须识别并使用双峰直方图。
是否有一个公式可以自动识别哪些直方图是双峰的,哪些不是?由于它们是数字向量,我可以使用编程语言(Java/C#)来处理它。
文献中有没有软件识别双峰直方图的标准?
以下是我正在使用的直方图和格式输入的 3 个示例。每个直方图都是一个具有 256 (0...255) 个位置的向量。
Histogram 1
8029, 41, 82, 177, 135, 255, 315, 591, 949, 456, 499, 688, 446, 733, 712, 1595, 2633, 3945, 6134, 9755, 9236, 11911, 11888, 9450, 13119, 8819, 5991, 4399, 6745, 2017, 3747, 1777, 2946, 1623, 2151, 454, 3015, 3176, 2211, 1080, 391, 580, 750, 473, 10424, 334, 559, 621, 340, 2794, 1094, 5274, 2822, 204, 389, 728, 268, 15, 1060, 58, 113, 2728, 52, 3166, 11, 103, 522, 107, …Run Code Online (Sandbox Code Playgroud) 查看元数据我发现了这个函数:(在"System.Convert"类中)
//
// Summary:
// Calling this method always throws System.InvalidCastException.
//
// Parameters:
// value:
// The date and time value to convert.
//
// Returns:
// This conversion is not supported. No value is returned.
//
// Exceptions:
// System.InvalidCastException:
// This conversion is not supported.
public static bool ToBoolean(DateTime value);
Run Code Online (Sandbox Code Playgroud)
为什么微软这样做?
这个样本:
public static void createDictionary<T>(IEnumerable<T> myRecords)
where T: T.ID // Wont Compile
{
IDictionary<int, T> dicionario = myRecords.ToDictionary(r => r.ID);
foreach (var item in dicionario)
{
Console.WriteLine("Key = {0}",item.Key);
Type thisType = item.Value.GetType();
StringBuilder sb = new StringBuilder();
foreach (var itemField in thisType.GetProperties())
{
sb.AppendLine(string.Format("{0} = {1}", itemField.Name, itemField.GetValue(item.Value, null)));
}
Console.WriteLine(sb);
}
}
Run Code Online (Sandbox Code Playgroud)
如何强制传递的类型作为参数有一个名为"ID"的字段?
此代码未编译,它抛出以下错误:
'TestesInterfaces.MyCollection'类型已包含'Current'的定义
但是当我删除模糊方法时,它会不断给出其他错误.
有人可以帮忙吗?
public class MyCollection<T> : IEnumerator<T>
{
private T[] vector = new T[1000];
private int actualIndex;
public void Add(T elemento)
{
this.vector[vector.Length] = elemento;
}
public bool MoveNext()
{
actualIndex++;
return (vector.Length > actualIndex);
}
public void Reset()
{
actualIndex = -1;
}
void IDisposable.Dispose() { }
public Object Current
{
get
{
return Current;
}
}
public T Current
{
get
{
try
{
T element = vector[actualIndex];
return element;
}
catch (IndexOutOfRangeException e)
{
throw …Run Code Online (Sandbox Code Playgroud) 我在Microsoft文档中找到以下内容:
Two objects that are equal return hash codes that are equal. However, the reverse is not true: equal hash codes do not imply object equality, because different (unequal) objects can have identical hash code
Run Code Online (Sandbox Code Playgroud)
我做了自己的测试来理解方法:
public static void HashMetod()
{
List<Cliente> listClientTest = new List<Cliente>
{
new Cliente { ID = 1, name = "Marcos", Phones = "2222"}
};
List<Empresa> CompanyList = new List<Empresa>
{
new Empresa { ID = 1, name = "NovaQuimica", Clients = listClientTest },
new …Run Code Online (Sandbox Code Playgroud) "假设以下代码:
public class MultiplasHerancas
{
static GrandFather grandFather = new GrandFather();
static Father father = new Father();
static Child child = new Child();
public static void Test()
{
grandFather.WhoAreYou();
father.WhoAreYou();
child.WhoAreYou();
GrandFather anotherGrandFather = (GrandFather)child;
anotherGrandFather.WhoAreYou(); // Writes "I am a child"
}
}
public class GrandFather
{
public virtual void WhoAreYou()
{
Console.WriteLine("I am a GrandFather");
}
}
public class Father: GrandFather
{
public override void WhoAreYou()
{
Console.WriteLine("I am a Father");
}
}
public class Child : Father …Run Code Online (Sandbox Code Playgroud) c# ×8
.net ×3
generics ×3
oop ×2
hashcode ×1
histogram ×1
inheritance ×1
object ×1
types ×1
yield-return ×1