小编naw*_*fal的帖子

哪个性能更高:<= 0或<1?

回到我学习C和装配的那一天,我们被教导,最好使用简单的比较来提高速度.例如,如果你说:

if(x <= 0)
Run Code Online (Sandbox Code Playgroud)

if(x < 1)
Run Code Online (Sandbox Code Playgroud)

哪个会执行得更快?我的论点(可能是错误的)是第二个几乎总是执行得更快,因为只有一个比较)即小于一,是或否.

如果数字小于0,第一个将快速执行,因为这等于为真,没有必要检查等于使得它与第二个一样快,但是,如果数字为0或更多,它将总是更慢,因为它然后必须进行第二次比较,看它是否等于0.

我现在正在使用C#,而在开发台式机时速度不是问题(至少没有达到他的观点值得争论的程度),我仍然认为这些论点需要考虑因为我也在为移动设备开发功能不如台式机,速度确实成为这类设备的问题.

为了进一步考虑,我说的是整数(没有小数)和数字,其中不能有负数如-1或-12,345等(除非有错误),例如,当你不能处理列表或数组时有一个负数的项目,但你想检查一个列表是否为空(或如果有问题,将x的值设置为负表示错误,一个例子是列表中有一些项目,但你不能由于某种原因检索整个列表,并指出你将数字设置为负数,这与说没有项目是不一样的).

由于上述原因,我故意忽略了显而易见的事实

if(x == 0)
Run Code Online (Sandbox Code Playgroud)

if(x.isnullorempty())
Run Code Online (Sandbox Code Playgroud)

用于检测没有项目的列表的其他此类项目.

同样,为了考虑,我们讨论的是从数据库中检索项目的可能性,可能使用具有所述功能的SQL存储过程(即标准(至少在该公司中)是返回负数以指示问题).

所以在这种情况下,使用上面的第一个或第二个项目会更好吗?

c# performance

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

为什么foreach调用GetHashCode?

当我在使用旅行时发现我GetHashCode()的类的覆盖方法Foo被调用时,我感到很惊讶 .但在其他情况下不会发生这种情况.为什么?foreachIEnumerable<Foo>

实际代码的某些部分:

var allVolumeImagesInvolvedInMerge = volumeChainsToMerge
    .SelectMany(x => x);

var allVolumeImagesNotInvolvedInMerge = allVolumeImagesWithinCell
    .Except(allVolumeImagesInvolvedInMerge)
    .Where(vi => volumeImagesNotAllowedToDelete.ContainsFast(vi) == false);

var volumeImagesCandidatesForDeletion = allVolumeImagesNotInvolvedInMerge
    .Where(x => driverVolumeIds.Contains(x.DriverVolumeId));

var groupedVolumeImagesCandidatesForDeletion = volumeImagesCandidatesForDeletion
    .GroupBy(vi => vi.DriverVolumeId);

// here GetHashCode is called
foreach (var group in groupedVolumeImagesCandidatesForDeletion)
{
   ...
}
Run Code Online (Sandbox Code Playgroud)

.net c# linq gethashcode

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

将Func <Type,object>表达式转换为Func <T>,其中T是通用约束

我有这个静态功能

public static object Create(Type t)
{
    //unimportant
}
Run Code Online (Sandbox Code Playgroud)

我无法控制上面的上述功能,所以我无法改变它.问题是它不是通用的,所以我必须将返回的对象转换为某种类型.这个类型是由我调用该Create方法的另一个泛型类的约束提供的.

这是我到达的地方:

public static class Creator<T>
{
    public static void Create()
    {
        var m = typeof(SomeClass).GetMethod("Create");
        var p = Expression.Parameter(typeof(Type));
        var e = Expression.Call(m, p);

        //at this stage I want to create delegate for calling the 'Create' method, 
        //then pass typeof(T) as parameter, get returned object, 
        //and finally cast it to 'T'.

        //for eg, I can do it like this:
        var f = Expression.Lambda<Func<Type, object>>(e, p).Compile();
        Func<T> mainThing …
Run Code Online (Sandbox Code Playgroud)

c# generics typeconverter expression-trees

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

使用运行时已知的类型创建委托

如何创建仅在运行时已知类型的委托?

我想做以下事情:

Type type1 = someObject.getType();
Type type2 = someOtherObject.getType();   

var getter = (Func<type1, type2>)Delegate.CreateDelegate(
    typeof(Func<,>).MakeGenericType(type1, type2), someMethodInfo);
Run Code Online (Sandbox Code Playgroud)

我怎样才能达到类似的效果?

.net c# delegates

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

如何执行多种样式的模式匹配?

刚开始玩F#.虽然现在和我一样可怕,但我也不知道要搜索类似的线程.

这就是我想要做的:

let test animal =
    if animal :? Cat //testing for type
    then "cat" 
    elif animal :? Dog //testing for type
    then "dog" 
    elif animal = unicorn //testing value equality
    then "impossible"
    else "who cares"
Run Code Online (Sandbox Code Playgroud)

基本上它涉及类型测试模式匹配以及其他条件检查.我可以像这样完成第一部分(类型检查):

let test(animal:Animal) =
    match animal with
    | :? Cat as cat -> "cat"
    | :? Dog as dog -> "cat"
    | _ -> "who cares"
Run Code Online (Sandbox Code Playgroud)

1.我是否可以在上述类型测试模式匹配中包含相等性检查(如第一个示例中所示)?

2.在F#圈中,在单一模式匹配结构中执行的这种多种检查通常是不受欢迎的吗?

f# pattern-matching

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

如何将两个数组的元素等同,而不管条目的顺序如何

我试图计算两个像myArray{a,b,c}和的数组urArray{a,b,c,c}

我想检查两个元素是否都有相同的元素,例如在上面的条件中,第二个数组urArray有一个额外的'c'.

如果代码具有相同的元素,则代码应该能够等同两组数组,并且元素的顺序无关紧要.只是两个数组都应该有相同的元素,即如果一个有两个'c',另一个也应该有两个'c',否则条件是假的.

所以我做的是:

char[] myArray = new char[] {'a','b','c','c'};
char[] urArray = new char[] { 'a', 'b', 'c' ,'a'};
List<char> tmp2 = new List<char>(urArray);

for (int i = 0; i < myArray.Length; ++i)
{
    for (int j = 0; j < urArray.Length; ++j)
    {
        if (myArray[i] == urArray[j])
        {

            Console.WriteLine(urArray[j] + "--> " + "urArray"+"  myArray"+"--> "+myArray[i]);
            tmp2.Remove(urArray[j]);

            urArray = tmp2.ToArray();

        }
        else if (myArray[i] != urArray[j])
        {
            Console.WriteLine(myArray[i] + …
Run Code Online (Sandbox Code Playgroud)

c# arrays comparison equality

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

无法从“System.Collections.Generic.IEnumerable&lt;int&gt;”转换为“System.Collections.Generic.IEnumerable&lt;int?&gt;”

class first
{
    private int? firstID;
}

class second
{
    private int secondID;
    private int secondField;
}

public override Expression<Func<first, bool>> FirstFilter()
{
    Contex db = new Contex();
    List<second> list = (from p in db.second select p).ToList();

    return b => list.Select(p => p.secondID).Contains(b.firstID);
}
Run Code Online (Sandbox Code Playgroud)

我有错误:

无法从“System.Collections.Generic.IEnumerable”转换为“System.Collections.Generic.IEnumerable”

我尝试了很多不同的方法,但我只是不知道如何解决它。

c# linq

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

如何获取datagridview/datagridviewcell的编辑控件?

我有一个datagridviewcell,文本框作为它托管的控件.现在我如何在代码的其他部分以编程方式获得控件类型?

我像这样添加列:

DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
col.ReadOnly = false;
col.Name = "Status";
col.HeaderText = "Status";
dgv.Columns.Add(col);
Run Code Online (Sandbox Code Playgroud)

该列中的所有单元格现在都有一个文本框.我可以将控件作为这样的文本框:

private void dgv_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    if (dgv.CurrentCell.ColumnIndex == 5 && e.Control is TextBox)
    {
         //something
    }
}
Run Code Online (Sandbox Code Playgroud)

如何获取其他位置的单元格中托管的控件类型?如何e.Control从代码的其他部分获取,以便我可以执行以下操作:

((TextBox)dgv[i, j].EditinControl).AutoCompleteSource = AutoCompleteSource.CustomSource;
((TextBox)dgv[i, j].EditinControl).AutoCompleteCustomSource = someSource;
((TextBox)dgv[i, j].EditinControl).AutoCompleteMode = AutoCompleteMode.SuggestAppend;
Run Code Online (Sandbox Code Playgroud)

什么可以代替EditinControl上面的那一行.. ??

.net datagridview winforms datagridviewtextboxcell

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

为什么我的intellisense没有列出HttpWebRequest.GetResponse方法?

using System;
using System.Net;
using System.Xml.Linq;


namespace PhoneApp1
{
    public class ABC
    {
        //constructor

        public ABC()
        {


        }

        void abc()
        {
            String url = "";

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse resp = (HttpWebResponse)req.GetRepsonse(); //why a wiggly line here?
            XDocument Xmldoc = XDocument.Load(url);
        }
Run Code Online (Sandbox Code Playgroud)

我没有得到GetResponse()这个req对象.为什么会这样?

这个链接和许多链接说它应该是可能的.

c# httpwebrequest getresponse windows-phone-7

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

清除集合或重新初始化?

我正在开发一个.net应用程序,我正在使用一个由5个Lists和1个Hashtable组成的对象.此对象在循环中使用,该循环至少迭代500次以运行某些分析.在每个循环中,这个对象应该从空开始,所以我想知道在所有Lists和Hashtable上调用clear是否更有效,或者我应该重新初始化对象?

我知道我可以编写代码来对此进行基准测试,但我想知道是否有人已经走了这条道路?

谢谢.

.net collections performance

0
推荐指数
2
解决办法
912
查看次数