Returning unique_ptr from functions

is there some other clause in the language specification that this exploits? Yes, see 12.8 §34 and §35: When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object […] This elision of copy/move operations, called copy elision, is permitted […] in a return statement in a function … Read more

Calculate the execution time of a method

Stopwatch is designed for this purpose and is one of the best ways to measure time execution in .NET. var watch = System.Diagnostics.Stopwatch.StartNew(); // the code that you want to measure comes here watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; Do not use DateTime to measure time execution in .NET. UPDATE: As pointed out by @series0ne in … Read more

PictureBox PaintEvent with other method

You need to decide what you want to do: Draw into the Image or draw onto the Control? Your code is a mix of both, which is why it doesn’t work. Here is how to draw onto the Control: private void pictureBox1_Paint(object sender, PaintEventArgs e) { e.Graphics.DrawEllipse(Pens.Red, new Rectangle(3, 4, 44, 44)); .. } Here … Read more

Passing data between forms

Try adding a parameter to the constructor of the second form (in your example, Form1) and passing the value that way. Once InitializeComponent() is called you can then add the parameter to the listbox as a choice. public Form1(String customItem) { InitializeComponent(); this.myListBox.Items.Add(customItem); } // In the original form’s code: Form1 frm = new Form1(this.textBox.Text);

Line delimited json serializing and de-serializing

You can do so by manually parsing your JSON using JsonTextReader and setting the SupportMultipleContent flag to true. If we look at your first example, and create a POCO called Foo: public class Foo { [JsonProperty(“some”)] public string Some { get; set; } } This is how we parse it: var json = “{\”some\”:\”thing1\”}\r\n{\”some\”:\”thing2\”}\r\n{\”some\”:\”thing3\”}”; var … Read more

Detect if deserialized object is missing a field with the JsonConvert class in Json.NET

The Json.Net serializer has a MissingMemberHandling setting which you can set to Error. (The default is Ignore.) This will cause the serializer to throw a JsonSerializationException during deserialization whenever it encounters a JSON property for which there is no corresponding property in the target class. static void Main(string[] args) { try { JsonSerializerSettings settings = … Read more

Calculating pow(a,b) mod n

You can try this C++ code. I’ve used it with 32 and 64-bit integers. I’m sure I got this from SO. template <typename T> T modpow(T base, T exp, T modulus) { base %= modulus; T result = 1; while (exp > 0) { if (exp & 1) result = (result * base) % modulus; … Read more