Category Archives: C#

Searching for a specific JToken by name in a JObject hierarchy

Questions: I have some Json response from server, for example: {“routes” : [ { “bounds” : { “northeast” : { “lat” : 50.4639653, “lng” : 30.6325177 }, “southwest” : { “lat” : 50.4599625, “lng” : 30.6272425 } }, “copyrights” : “Map data ©2013 Google”, “legs” : [ { “distance” : { “text” : “1.7 km”,… Read More »

operators as strings

Questions: I need to evaluate a mathmatical expression that is presented to me as a string in C#. Example noddy but gets the point across that the string as the expression. I need the evaluate to then populate an int. There is no Eval() in C# like in others langugaes… String myString = “3*4”; Edit:… Read More »

How to resize multidimensional (2D) array in C#?

Questions: I tried the following but it just returns a screwed up array. T[,] ResizeArray<T>(T[,] original, int rows, int cols) { var newArray = new T[rows,cols]; Array.Copy(original, newArray, original.Length); return newArray; } Answers: Most methods in the array class only work with one-dimensional arrays, so you have to perform the copy manually: T[,] ResizeArray<T>(T[,] original,… Read More »

How to implement real time data for a web page

Questions: (This is intended as a Q/A style question, intended to be a go-to resource for people that ask similar questions. A lot of people seem to stumble on the best way of doing this because they don’t know all the options. Many of the answers will be ASP.NET specific, but AJAX and other techniques… Read More »

Captured Closure (Loop Variable) in C# 5.0

Questions: This works fine (means as expected) in C# 5.0: var actions = new List<Action>(); foreach (var i in Enumerable.Range(0, 10)) { actions.Add(() => Console.WriteLine(i)); } foreach (var act in actions) act(); Prints 0 to 9. But this one shows 10 for 10 times: var actions = new List<Action>(); for (var i = 0; i… Read More »

Why are properties without a setter not serialized

Questions: I have a serializable class and one of the properties in my class generates a Guid in the getter. The property implements no setter and is ignores during serialization. Why is that and do I always have to implement a setter in order for my property to be serialized. [Serializable] public class Example {… Read More »

Category: C#

Add new item in existing array in c#.net

Questions: How to add new item in existing string array in c#.net. i need to preserve the existing data. Answers: I would use a List if you need a dynamically sized array: List<string> ls = new List<string>(); ls.Add(“Hello”); Questions: Answers: That could be a solution; Array.Resize(ref array, newsize); array[newsize – 1] = “newvalue” But for… Read More »