小编Mic*_*ows的帖子

如何使用LINQ(C#3.0)更改IDictionary的内容

如何使用C#3.0(Linq,Linq扩展)更改IDictionary的内容?

var enumerable = new int [] { 1, 2};
var dictionary = enumerable.ToDictionary(a=>a,a=>0);
//some code
//now I want to change all values to 1 without recreating the dictionary
//how it is done?
Run Code Online (Sandbox Code Playgroud)

.net c# linq idictionary c#-3.0

5
推荐指数
1
解决办法
1428
查看次数

多次实例化类的性能问题

我想知道在表单中或在需要时实例化一次类之间的性能差异.例如,假设我有一个客户表单来编辑客户.在表单加载上,我实例化一个客户类并调用一个函数来返回客户数据以填充表单控件.

CustomerInfo customer = new CustomerInfo();
CustomerDetail  cust = customer.GetCustomer(customerId);
txtName. cust.Name;
...
Run Code Online (Sandbox Code Playgroud)

表单上还有一个保存按钮.单击该按钮时,我创建Customer类的另一个实例以更新数据.

CustomerDetail cust = new CustomerData();
cust.Id = customerId;
cust.Name = txtName.Text;

CustomerInfo customer = new CustomerInfo();
customer.Update(cust);
Run Code Online (Sandbox Code Playgroud)

我知道这很好用.但是,性能方面是否更好,只需为整个表单创建Customer类的单个实例以调用GetCustomer和Update?我知道GC将处理这些实例,但我不确定它会破坏第一个实例然后继续下一个实例.

此外,这个例子我只使用两个函数调用客户类,但实际上,可能会有更多.

谢谢您的帮助.

.net c# performance

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

如何在C#中加载我的测试数据?

我正在努力找到一种在C#中加载测试数据的简单方法.

在Java中,我使用以下代码加载资源:

...

public static InputStream loadResource(String resource) throws LoadException {
    InputStream is = TestUtils.class.getResourceAsStream(resource);

    if (is == null) {
        throw new LoadException("Error loading '" + resource + "'");
    }

    return is;
}

...

public static void main(String[] args) {
    InputStream is = TestUtils.loadResource("/resourcelocation");
}
Run Code Online (Sandbox Code Playgroud)

我试图使用C#资源文件,但我发现加载和操作它很尴尬.有没有更简单的方法来加载C#中的资源?

c# testing nunit

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

类隐式转换

我知道我可以使用类的隐式转换,如下所示,但有没有办法让一个实例返回没有强制转换或转换的字符串?

public class Fred
{
    public static implicit operator string(Fred fred)
    {
        return DateTime.Now.ToLongTimeString();
    }
}

public class Program
{
    static void Main(string[] args)
    {
        string a = new Fred();
        Console.WriteLine(a);

        // b is of type Fred. 
        var b = new Fred(); 

        // still works and now uses the conversion
        Console.WriteLine(b);    

        // c is of type string.
        // this is what I want but not what happens
        var c = new Fred(); 

        // don't want to have to cast it …
Run Code Online (Sandbox Code Playgroud)

c# casting type-inference implicit-typing

0
推荐指数
1
解决办法
208
查看次数

如何在方法中向下转换ref变量

我需要在一个方法中将long转发为int,其中long作为ref变量传递:

public void Foo(ref long l)
{
    // need to consume l as an int
}
Run Code Online (Sandbox Code Playgroud)

我怎么能轻松做到这一点?

.net c# casting ref-parameters

0
推荐指数
1
解决办法
415
查看次数