c#-4.0

Get N max numbers and their corresponding position(index) from a 2D array double[,] using lambda expression

穿精又带淫゛_ 提交于 2020-01-14 04:04:08
问题 This is a question similar to [ Get N max numbers from a List<int> using lambda expression ] But I want to learn if I want to keep the index of those N max numbers, how should I write it using lambda expression. Example) List<int> numbers = new List<int> { 12, 5, -8, 4, 7, 28, 3, 22 }; How can we get 4 maximum numbers by lambda: {28, 22, 12, 7} plus indexes { 5, 7, 0, 4} as kirill suggested: var result = numbers.OrderByDescending(n => n).Take(4); but how can I get the index of those N max

Changing dynamically created button's background in WPF

こ雲淡風輕ζ 提交于 2020-01-14 03:16:08
问题 I have the following code to dynamically create and add a button to a panel: StackPanel topPanel=...; Button button=new Button(); button.Content="New Button "+topPanel.Children.Count; // Set button background to a red/yellow linear gradient // Create a default background brush var bgBrush=new LinearGradientBrush(new GradientStopCollection( new GradientStop[] {new GradientStop(Color.FromRgb(255,255,200),0.5), new GradientStop(Color.FromRgb(255,200,200),0.5)})); // Create a more intense mouse

Is it possible to serialize a grouped list?

早过忘川 提交于 2020-01-13 20:35:07
问题 I want to serialize grouped list. but I am getting error. Is it possible to serialize a grouped list? if yes then How? Error : Cannot serialize interface System.Linq.IGrouping`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[MyProject.MyNamespace.Elements, MyProject.MyNamespace, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]. Code : MemoryStream memoryStream = new MemoryStream(); List<IGrouping<string, Elements>> lstGroupedElements =

RegEx help to remove noise words or stop words from string

和自甴很熟 提交于 2020-01-13 18:19:27
问题 I want to remove all noise tags from input tags (a string) The tags are separated by comma. If a noise word is part of a big tag, it will remain. This is what I have but not working: string input_string = "This,sure,about,all of our, all, values"; string stopWords = "this|is|about|after|all|also"; stopWords = string.Format(@"\s?\b(?:{0})\b\s?", stopWords); string tags = Regex.Replace(input_string, stopWords, "", RegexOptions.IgnoreCase); This is what I want from above input: ",sure,,all of

A better way to write extension method to invoke a control?

女生的网名这么多〃 提交于 2020-01-13 18:14:31
问题 I have this general function to invoke a WinForm control: public static void Invoke(this Control c, Action action) { if (c.InvokeRequired) c.TopLevelControl.Invoke(action); else action(); } I'm thinking of making it better by bringing harsher constraints to prevent things that are meaningless, may be like: button1.Invoke(() => list.Add(1)); Also there can be redundant typing like: button1.Invoke(() => button1.Hide()); since we are already specifying the this is button1 . So I made it: public

Why is this string not equal when it is decrypted a second time with AesCryptoServiceProvider?

天涯浪子 提交于 2020-01-13 17:07:14
问题 I'm having trouble with encryption and decryption of text in C# (VS2012, .NET 4.5). Specifically, When I encrypt and subsequently decrypt a string, the output is not the same as the input. However, bizarrely, if I copy the encrypted output and hardcode it as a string literal, decryption works. The following code sample illustrates the problem. What am I doing wrong? var key = new Rfc2898DeriveBytes("test password", Encoding.Unicode.GetBytes("test salt")); var provider = new

How can we create a Singleton Instance for a Window?

放肆的年华 提交于 2020-01-13 13:12:36
问题 I have searched for creating a Singleton object for a window in WPF. public static Test DefInstance { get { if (formDefInstance == null) // formDefInstance.IsDisposed { initializingDefInstance = true; formDefInstance = new cas18(); initializingDefInstance = false; } return formDefInstance; } set { formDefInstance = value; } } But the forDefInstance.IsDisposed is not working and throwing an error. Any Idea regarding this? 回答1: I think everyone should take a look at Jon Skeet's C# In Depth site

how can i hide html list item <li> using c# from code behind

爷,独闯天下 提交于 2020-01-13 11:39:32
问题 I want to hide html list item that is "li" tag using C#. But i can't do this. In earlier i just hide DIV tag using c#. But i can't hide "li" tag. Please help me to do this .If you can please send your detail Explanation... This is my partial code : this.hide.style.Add("display", "none"); // Error in hide This is my html code : <li ID="hide" style="display: Block;"><a href="../list.aspx" >list Approval</a></li> Please help me to solve this issue .... 回答1: Add an id and runat="server" to your

MEF ComposeParts. How to handle plugin exceptions

独自空忆成欢 提交于 2020-01-13 11:26:46
问题 I have searched on the web for a solution, but I didn't find anything. In my C# application I am using MEF for implementing a plugin pattern. Everything is working fine. However today I have tried to figure out what happens if a Plugin Constructor throws an Exception for some reason. To load plugins I am using CompositionContainer.ComposeParts . If for some reason one of the X plugins throws an exception this method will fail and nothing will be loaded. Is there a way to just catch the single

Task.ContinueWith() parent task doesn't wait for child task to finish [duplicate]

邮差的信 提交于 2020-01-13 10:12:12
问题 This question already has answers here : Task.WaitAll doesn't wait for child task? (2 answers) Closed 5 years ago . Since I was understanding the Task in context of nested task, I really don't understand that- Why the 3rd print before 2nd print? Even though, I have used Task.WaitAll(t) , it print 3rd line before 2nd line. Code: public static void Main() { Task t = new Task( () => { Thread.Sleep(2000); Console.WriteLine("1st print..."); }); t.ContinueWith( x => { Thread.Sleep(2000); Console