.NET / C# – Convert char[] to string
There’s a constructor for this: char[] chars = {‘a’, ‘ ‘, ‘s’, ‘t’, ‘r’, ‘i’, ‘n’, ‘g’}; string s = new string(chars);
There’s a constructor for this: char[] chars = {‘a’, ‘ ‘, ‘s’, ‘t’, ‘r’, ‘i’, ‘n’, ‘g’}; string s = new string(chars);
Json.NET allows us to do this: dynamic d = JObject.Parse(“{number:1000, str:’string’, array: [1,2,3,4,5,6]}”); Console.WriteLine(d.number); Console.WriteLine(d.str); Console.WriteLine(d.array.Count); Output: 1000 string 6 Documentation here: LINQ to JSON with Json.NET See also JObject.Parse and JArray.Parse
No, it’s not. Use the Stopwatch (in System.Diagnostics) Stopwatch sw = Stopwatch.StartNew(); PerformWork(); sw.Stop(); Console.WriteLine(“Time taken: {0}ms”, sw.Elapsed.TotalMilliseconds); Stopwatch automatically checks for the existence of high-precision timers. It is worth mentioning that DateTime.Now often is quite a bit slower than DateTime.UtcNow due to the work that has to be done with timezones, DST and such. … Read more
You’ll want something like this to increase the message size quotas, in the App.config or Web.config file: <bindings> <basicHttpBinding> <binding name=”basicHttp” allowCookies=”true” maxReceivedMessageSize=”20000000″ maxBufferSize=”20000000″ maxBufferPoolSize=”20000000″> <readerQuotas maxDepth=”32″ maxArrayLength=”200000000″ maxStringContentLength=”200000000″/> </binding> </basicHttpBinding> </bindings> And use the binding name in your endpoint configuration e.g. … bindingConfiguration=”basicHttp” … The justification for the values is simple, they are sufficiently … Read more
The compiler replaces null comparisons with a call to HasValue, so there is no real difference. Just do whichever is more readable/makes more sense to you and your colleagues.
How about this: int timeout = 1000; var task = SomeOperationAsync(); if (await Task.WhenAny(task, Task.Delay(timeout)) == task) { // task completed within timeout } else { // timeout logic } And here’s a great blog post “Crafting a Task.TimeoutAfter Method” (from MS Parallel Library team) with more info on this sort of thing. Addition: at … Read more
int[] ids = new[] { 1, 2, 3, 4, 5 }; Parallel.ForEach(ids, i => DoSomething(1, i, blogClient).Wait()); Although you run the operations in parallel with the above code, this code blocks each thread that each operation runs on. For example, if the network call takes 2 seconds, each thread hangs for 2 seconds w/o doing … Read more
If you want to unit test a private method, something may be wrong. Unit tests are (generally speaking) meant to test the interface of a class, meaning its public (and protected) methods. You can of course “hack” a solution to this (even if just by making the methods public), but you may also want to … Read more
A lot of people are saying that once you get to the size where speed is actually a concern that HashSet<T> will always beat List<T>, but that depends on what you are doing. Let’s say you have a List<T> that will only ever have on average 5 items in it. Over a large number of … Read more
It’s not only necessary to use the namespace System.Configuration. You have also to add the reference to the assembly System.Configuration.dll , by Right-click on the References / Dependencies Choose Add Reference Find and add System.Configuration. This will work for sure. Also for the NameValueCollection you have to write: using System.Collections.Specialized;