小编use*_*675的帖子

C#Sort和OrderBy比较

我可以使用Sort或OrderBy对列表进行排序.哪一个更快?两者都在使用相同的算法吗?

List<Person> persons = new List<Person>();
persons.Add(new Person("P005", "Janson"));
persons.Add(new Person("P002", "Aravind"));
persons.Add(new Person("P007", "Kazhal"));
Run Code Online (Sandbox Code Playgroud)

1.

persons.Sort((p1,p2)=>string.Compare(p1.Name,p2.Name,true));
Run Code Online (Sandbox Code Playgroud)

2.

var query = persons.OrderBy(n => n.Name, new NameComparer());

class NameComparer : IComparer<string>
{
    public int Compare(string x,string y)
    {
      return  string.Compare(x, y, true);
    }
}
Run Code Online (Sandbox Code Playgroud)

.net c# sorting performance sql-order-by

98
推荐指数
5
解决办法
10万
查看次数

C#lambda表达式反向<=

我见过一些使用<=运算符的代码.你能解释一下反向使用lambda的用途吗?

c# lambda operators

73
推荐指数
3
解决办法
6944
查看次数

如何创建通用扩展方法?

我想开发一个通用扩展方法,它应该按字母顺序排列字符串,然后按纵向升序排列.

我的意思是

string[] names = { "Jon", "Marc", "Joel",
                  "Thomas", "Copsey","Konrad","Andrew","Brian","Bill"};

var query = names.OrderBy(a => a.Length).ThenBy(a => a);
Run Code Online (Sandbox Code Playgroud)

开发通用扩展方法的方法是什么?

我试过了 :

public static class ExtensionOperation
    {
        public static T[] AlphaLengthWise<T>(this T[] names)
        {
            var query = names.OrderBy(a => a.Length).ThenBy(a => a);
            return query;
        }
    }
Run Code Online (Sandbox Code Playgroud)

我收到了 :

错误1:T不包含Length的定义

错误2:无法转换System.Linq.IOrderedEnumerableT[].

c# generics

63
推荐指数
3
解决办法
8万
查看次数

C#-Four模式在异步执行中

我听说异步执行有四种模式.

"异步委托执行有四种模式:轮询,等待完成,完成通知和"消防和忘记".

当我有以下代码时:

class AsynchronousDemo
{
    public static int numberofFeets = 0;
    public delegate long StatisticalData();

    static void Main()
    {
        StatisticalData data = ClimbSmallHill;
        IAsyncResult ar = data.BeginInvoke(null, null);
        while (!ar.IsCompleted)
        {
            Console.WriteLine("...Climbing yet to be completed.....");
            Thread.Sleep(200);

        }
        Console.WriteLine("..Climbing is completed...");
        Console.WriteLine("... Time Taken for  climbing ....{0}", 
        data.EndInvoke(ar).ToString()+"..Seconds");
        Console.ReadKey(true);

    }


    static long ClimbSmallHill()
    {
        var sw = Stopwatch.StartNew();
        while (numberofFeets <= 10000)
        {
            numberofFeets = numberofFeets + 100;
            Thread.Sleep(10);
        }
        sw.Stop();
        return sw.ElapsedMilliseconds;
    }
}
Run Code Online (Sandbox Code Playgroud)

1)上述代码实现的模式是什么?

2)你能解释一下代码,我该如何实现其余的代码?

c# asynchronous execution

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

AsyncWaitHandle.WaitOne的详细信息

1)调用AsyncWaitHandle.WaitOne可能会阻止客户端或肯定会阻止客户端?

2)WaitAll,WaitOne,WaitAny之间有什么区别?

c# multithreading

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

C# - 使用扩展方法排序

我想排序一个人说的名单

List<Person> persons=new List<Person>();
persons.Add(new Person("Jon","Bernald",45000.89));
persons.Add(new Person("Mark","Drake",346.89)); 
persons.Add(new Person("Bill","Watts",456.899));
Run Code Online (Sandbox Code Playgroud)

基于

public enum CompareOptions
 {
    ByFirstName,
    ByLastName,
    BySalary
 }

 public enum SortOrder
 {
   Ascending,
   Descending
 }
Run Code Online (Sandbox Code Playgroud)

使用lambda表达式排序的方法是什么?

    public static List<Person> SortPeople(this List<Person> lst, 
   CompareOptions opt1,SortOrder ord)

        {
           lst.Sort((p,op1,op2)=>{ how to apply lambda expression here});
        }
Run Code Online (Sandbox Code Playgroud)

c# extension-methods

10
推荐指数
2
解决办法
6301
查看次数

C#WaitCallBack - ThreadPool

WaitCallback委托的确切目的是什么?

WaitCallback callback = new WaitCallback(PrintMessage);
ThreadPool.QueueUserWorkItem(callback,"Hello");

static void PrintMessage(object obj)
{
   Console.WriteLine(obj);
}
Run Code Online (Sandbox Code Playgroud)

我可以在"TheadPool"中表示"等待",直到线程可用.一旦可用,执行目标?

.net c# multithreading

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

C#异步操作

实际上,我很难理解BeginInvoke()和EndInvoke()对.

class AsynchronousDemo
{

    public delegate void DemoDelegate();
    static void Main()
    {

        DemoDelegate d = PrintA;

        IAsyncResult AResult = d.BeginInvoke(Callback,null);
        d.EndInvoke(AResult);
        Console.ReadKey(true);
    }

    static void PrintA()
    {
        Console.WriteLine("....Method in Print A Running ....");
        Thread.Sleep(4000);
        Console.WriteLine("....Method in Print A Completed...");
    }


    static void Callback(IAsyncResult ar)
    {
        Console.WriteLine("I will be finished after method A 
        completes its execution");
    }
}
Run Code Online (Sandbox Code Playgroud)

1)我们使用"EndInvoke()"来表示BeginInvoke()的结束"异步操作"吗?

2)这对配对的实际用途是什么?

3)我可以得到一些简单而好的例子来更恰当地理解它吗?

c# multithreading

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

C#随机对生成

假设掷骰子的可能结果是{1,2,3,4,5,6}中的一个

两个骰子被扔三次时,我想收集两个骰子的随机结果.

我的实施是

var q = from threeTimes in new int[] { 1, 2, 3 }
                    let set1 = new Random().Next(1, 6)
                    let set2 = new Random().Next(1, 6)
                    select new { s1 = set1, s2 = set2 };

            foreach (var v in q)
            {
                Console.WriteLine("Die1 :{0} Die2 :{1}", v.s1, v.s2);
            }
Run Code Online (Sandbox Code Playgroud)

但是大多数时候我收到Die1和Die2的相同值.

我的意思是

Die1: 5   Die2:  5

Die1: 2   Die2:  2

Die1: 2   Die2:  2
Run Code Online (Sandbox Code Playgroud)

为获得随机对,我需要进行哪些修正?

c# linq

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

C#LINQ用有意义的字符串替换空值

从列表中

class Delivery
{
    public string ProductCode
    {
        get;
        set;
    }

    public DateTime? OrderedDate
    {
        get;
        set;
    }

    public DateTime? DeliveryDate
    {
        get;
        set;
    }

    public Delivery(string pcode, DateTime? orddate, DateTime? deldate)
    {
        ProductCode = pcode;
        OrderedDate = orddate;
        DeliveryDate = deldate;
    }
}


List<Delivery> DeliveryList = new List<Delivery>();
DeliveryList.Add(new Delivery("P001",new DateTime(2009,01,27),null));
DeliveryList.Add(new Delivery("P007",new DateTime(2009,05,17),null));
DeliveryList.Add(new Delivery("P031", new DateTime(2008, 03, 15),
new DateTime(2008,04 ,22)));
DeliveryList.Add(new Delivery("P011",new DateTime(2009,01,27),
new DateTime(2009,02,12)));
DeliveryList.Add(new Delivery("P041",new DateTime(2009,01,27),null));
DeliveryList.Add(new Delivery("P051", new DateTime(2009, 01, 27),
new DateTime(2009, …
Run Code Online (Sandbox Code Playgroud)

c# linq

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