小编Rah*_*han的帖子

通用构造函数:T entity = new T();

我有以下winforms类:

class EntityEditorForm<T>: System.Windows.Forms.Form 
                              where T: ICloneable<T> {}

class EntityCollectionEditorForm<T> : System.Windows.Forms.Form 
                                      where T: ICloneable<T> {}
Run Code Online (Sandbox Code Playgroud)

第一个表单类是一个编辑器,用于<T>在运行时根据T的类型创建控件.

第二个是一个管理器,用于收集<T>和添加,编辑和删除功能.该集合显示在listview控件中,其中使用自定义属性通过反射填充字段.

"添加"和"编辑"按钮的代码如下所示:

private void buttonEdit_Click (object sender, System.EventArgs e)  
{  
   T entity = default(T);  
   entity = (T) this.listView.SelectedItems[0].Tag;  
   new EntityEditor<T>(entity).ShowDialog(this);  
}

private void buttonEdit_Click (object sender, System.EventArgs e)  
{  
   T entity = new T();   //This is the code which is causing issues 
   entity = (T) this.listView.SelectedItems[0].Tag;  
   new EntityEditor<T>(entity).ShowDialog(this);  
}
Run Code Online (Sandbox Code Playgroud)

default(T)在编辑的情况下工作,但我在与添加的场景麻烦.T entity = new T();看似不合法.

c# generics constructor constraints

3
推荐指数
1
解决办法
1605
查看次数

.NET循环完整性101

我一直对这个感到困惑.考虑以下循环:

int [] list = new int [] { 1, 2, 3 };  
for (int i=0; i < list.Length; i++) { }  
foreach (int i in list) { }  
while (list.GetEnumerator().MoveNext()) { } // Yes, yes you wouldn't call GetEnumerator with the while. Actually never tried that.  
Run Code Online (Sandbox Code Playgroud)
  • 上面的[list]是硬编码的.如果在循环进行迭代时列表是在外部更改的,那会发生什么?
  • 如果[list]是一个只读属性int List{get{return(new int [] {1,2,3});}}怎么办?这会扰乱循环吗?如果没有,它会在每次迭代中创建一个新实例吗?

c# ienumerable loops integrity

3
推荐指数
1
解决办法
111
查看次数

可以Parallel.For针对非常短时间运行进行优化吗?

我知道为短期运行任务提供细粒度控制的任务,但我有一种情况是使用foreach循环更自然.问题是,是否有可能告诉Parallel.For期望短时间运行并使用尽可能多的线程来最大化CPU?

如果没有,那么您建议并行化的方法是什么:

bool [,] grid = new bool [1000, 1000];
for (int y=0; y<1000; y++)
    for (int x=0; x<1000; x++)
        // Ignore the bounds error. This is just to illustrate a very short operation.
        grid[x, y] |= grid[x-1, y+1];
Run Code Online (Sandbox Code Playgroud)

.net c# parallel-processing task-parallel-library

3
推荐指数
1
解决办法
1049
查看次数

如果在C#中隐式重载,重载显式运算符是否有好处?

我正在使用一个结构,需要隐式运算符对字符串,并遇到一个我没有想过的基本问题.

public static implicit operator Version (string value) {...}
Run Code Online (Sandbox Code Playgroud)

我可以理解只有显式运算符来强制执行转换,但如果隐式运算符已经过载,则无法想到需要它的情况.有吗?

.net c# operator-overloading implicit-conversion explicit-conversion

3
推荐指数
1
解决办法
219
查看次数

根据其他列表或不同大小对列表进行排序

请考虑以下列表:

List<string> ordered = new List<string> (new string [] { "one", "two", "three", ... });
List<string> sequence1 = new List<string> (new string [] { "two", "three", "ten", ... });
Run Code Online (Sandbox Code Playgroud)

该列表[ordered]是包含所有可能值的超集.

该列表[sequence1]是列表的随机排序子集[ordered].它可能包含部分或全部超集的元素.

我目前有一个冗长的功能,[sequence1]基于此排序,[ordered]并想知道是否更简单的Linq方式来做到这一点.当前方法使用for循环迭代超集,并尝试在子集中查找值并将其移动到其相关位置.

列表中的项目数量永远不会超过几十个,并且将谨慎地调用排序操作,因此效率不是关键.

.net c# linq sorting

3
推荐指数
1
解决办法
911
查看次数

为什么IDictionary <TKey,TValue>扩展ICollection <KeyValuePair <TKey,TValue >>?

我正在尝试ReadOnlyDictionary<TKey, TValue>为.NET 4.0 创建自定义.方法是保留私有Dictionary<TKey, TValue>对象和标志,以确定是否允许添加/删除和项目分配.

这工作正常,但我想实现IDictionary<TKey, TValue>完整性的接口.但是,我注意到它扩展了,ICollection<KeyValuePair<TKey, TValue>>而它的属性或方法都没有出现在Dictionary<TKey, TValue>类中.这怎么可能?如果实现了接口,为什么ICollection不暴露成员?

此外,为什么Dictionary班级需要ICollection首先实施?

这是一个粗略的实现:

public sealed class ReadOnlyDictionary<TKey, TValue>:
    //IDictionary<TKey, TValue>,
    IEnumerable<KeyValuePair<TKey, TValue>>
{
    #region Members.

    public bool AllowListEdit { get; private set; }
    public bool AllowItemEdit { get; private set; }
    private Dictionary<TKey, TValue> Dictionary { get; set; }

    #endregion Members.

    #region Constructors.

    public ReadOnlyDictionary (bool allowListEdit, bool allowItemEdit) { this.AllowListEdit = allowListEdit; this.AllowItemEdit = allowItemEdit; …
Run Code Online (Sandbox Code Playgroud)

.net c# collections dictionary readonly

3
推荐指数
1
解决办法
1175
查看次数

C#中的矩阵/坐标转换

我有一组反映图像上已知位置的坐标.我们称之为模板图像.它有一个独特的条形码和方向标记(也在坐标数组中).

打印,扫描图像并将其反馈到我的应用程序中进行检测.在打印和扫描期间,图像可以以三种方式转换; 翻译,轮换和规模.

假设我可以在失真图像上找到方向标记,我如何使用矩阵变换来获得剩余坐标的相对位置?

之前在SO上发布了这个问题,但是让它太复杂,无法理解我想要的东西.

编辑

namespace MatrixTest
{
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections.Generic;

public static class Program
{
public static void Main ()
{
Template template = new Template(); // Original template image.
Document document = new Document(); // Printed and scanned distorted image.

template.CreateTemplateImage();

// The template image is printed and scanned. This method generates an example scan or this question.
document.CreateDistortedImageFromTemplateImage();
// Stuck here.
document.Transform();
// Draw transformed points on the image …
Run Code Online (Sandbox Code Playgroud)

c# transformation image matrix coordinates

2
推荐指数
1
解决办法
5932
查看次数

.NET中64位应用程序的性能优势(C#)

我正在将一个复杂的算法从32位移植到64位,在我的Core i5机器上运行大约5个小时,运行Windows 7 64位的8GB RAM.

该应用程序面向.NET 4,并使用任务并行库约60%的循环并使用BigInteger类.

我正在处理两个数字范围:

  • 0到ulong.MaxValue(大约50%的算法).
  • 具有数十万个数字的非常大的整数(约为算法的50%).

对这些数字执行的操作包括加法,减法乘法,除法,对数和幂.

一旦移植到64位,我将能够对代码进行分析和计时以查看性能提升,但我想知道我是否可以通过计算来估计它.

如果是这样,请推荐一些解释相同的文章.

.net c# optimization performance 64-bit

2
推荐指数
1
解决办法
2093
查看次数

T4 引擎无法识别当前项目名称空间

我正在使用 VS 2010 (C#) T4 模板来生成代码。

我需要迭代项目中的所有类型,列出实体 poco 类并生成包装器。问题是,项目名称空间未被识别。

这是解决方案的结构:

namespace MySolution.Entities
{
    public class Employee { ... }
    public class Department { ... }
}

// Seperate project referenceing MySolution.Entities.
namespace MySolution.Database
{
    public partial class Context { ... }

    // Should generate Context.cs as a partial class with after iterating Syste.Types available in MySolution.Entities.
    Context.tt
}
Run Code Online (Sandbox Code Playgroud)

这是文本模板:

<#@ template language="C#" #>
<#@ Output Extension=".cs" #>

namespace MySolution.Database
{
    public partial class Context:
        System.Data.Entity.DbContext
    {
<#
System.Type [] types …
Run Code Online (Sandbox Code Playgroud)

.net c# t4 self-reference entity-framework-5

2
推荐指数
1
解决办法
3585
查看次数

C#中的接口实现语法选项

好的,这是一个非常基本的问题,但我刚刚碰到它.考虑以下课程:

public class WindowComparer: IComparer, Generic.IComparer<Window> { }
Run Code Online (Sandbox Code Playgroud)

场景1:我通常会声明它的方式.

public int Compare (object x, object y)
{
    return (this.Compare((Window) x, (Window) y));
}

public int Compare (Window x, Window y) {...}
Run Code Online (Sandbox Code Playgroud)

场景2:MSDN文档通常声明它的方式.

int Collections.IComparer.Compare (object x, object y)
{
    // [this.Compare] is not recognized. How to access this?
    return (this.Compare((Window) x, (Window) y));
}

int Generic.IComparer<Window>.Compare (Window x, Window y) {...}
Run Code Online (Sandbox Code Playgroud)

但是,在尝试方案2时,i​​ntellisense会丢失Compare方法,并且编译器会引发一个错误,指出Compare方法不是WindowComparer的成员.我错过了什么?

.net c# generics syntax interface

2
推荐指数
1
解决办法
394
查看次数