Browsing 7239 questions and answers with Jon Skeet

hours difference not get accurate

Hi i am converting time to ESt using offset using below function public static DateTimeOffset ConvertToTheaterTimeZone2(string TheaterTimeZone, DateTimeOffset Date) ...
Jon Skeet
people
quotationmark

The time zone ID "Eastern Standard Time" doesn't actually mean "Eastern Standard Time". It really means "Eastern Time"... From TimeZoneInfo.Id: The value of the Id property is usually, but not always, identical to that of the... more 6/12/2014 2:01:32 PM

people

select all xml child nodes that start with a string or another string

I am using C# . I was given an xml node from out client with child nodes as follows : <PriceID>32</PriceID> <Store_1> 344</Store_1> ...
Jon Skeet
people
quotationmark

If you can use LINQ to XML, it's simple: var results = doc.Descendants() .Where(x => x.Name.LocalName.StartsWith("Store") || x.Name.LocalName.StartsWith("SS")); With XmlDocument it's... more 6/12/2014 1:56:59 PM

people

Create an arrayList from two other arrayList and keep only value that are identical in both arrayList

I have two arrayList that can contain 0 or many int and I need to create a new arrayList from these two with values that are in both. In this case [4,23] Example arrayList1 [3...
Jon Skeet
people
quotationmark

This would be more efficient with sets, but even just with lists, it's simple to use retainAll: // Start by copying arrayList1 List<Integer> result = new ArrayList<Integer>(arrayList1); result.retainAll(arrayList2); That's... more 6/12/2014 1:44:20 PM

people

Thread safe changes to a ConcurrentDictionary

I am populating a ConcurrentDictionary in a Parallel.ForEach loop: var result = new ConcurrentDictionary<int, ItemCollection>(); Parallel.ForEach(allRoutes, route =>...
Jon Skeet
people
quotationmark

I think you want GetOrAdd, which is explicitly designed to either fetch an existing item, or add a new one if there's no entry for the given key. var collection = result.GetOrAdd(someKey, _ => new... more 6/12/2014 1:22:22 PM

people

Is this possible to override method of sealed class?

In WinRT(C#, xaml) ScrollViewer - is sealed class, and I can't extend it, but I need to overwrite some methods (for ex: ScrollToHorizontalOffset). Is this possible to override...
Jon Skeet
people
quotationmark

No - in order to override a method, you have to derive from it, which you can't do when the class is sealed. Basically, you need to revisit your design to avoid this requirement... more 6/12/2014 1:07:30 PM

people

Force no BOM when saving XML

I'm saving a XML file that is used by an external tool. The tool sadly doesn't understand the encoding BOM (Byte Order Mark: EF BB BF) at the beginning of the file: <?xml...
Jon Skeet
people
quotationmark

You can create a UTF8Encoding instance which doesn't use the BOM, instead of using Encoding.UTF8. using (TextWriter sw = new StreamWriter(file, false, new UTF8Encoding(false))) { doc.Save(sw); } You can save this in a static field... more 6/12/2014 1:03:35 PM

people

Repeat an enumerable indefinitely

Is there an enumerable extension method that repeats the enumerable indefinitely? So for example, given an enumerable that returns: ["a", "b", "c"]. I would like a method that...
Jon Skeet
people
quotationmark

I don't know of anything built into LINQ, but it's really easy to create your own: public static IEnumerable<T> RepeatIndefinitely<T>(this IEnumerable<T> source) { while (true) { foreach (var item in... more 6/12/2014 10:06:09 AM

people

Check if XElement with particular value exist

For example for the following XML <Tree> <Order> <Phone>1254</Phone> <City>City1</City> ...
Jon Skeet
people
quotationmark

There's an Any overload which accepts a predicate, so you just need to use that: bool exists = Tree.Elements("Order") .Elements("City") .Any(x => x.Value == "City2"); Alternatively, use Where... more 6/12/2014 10:03:17 AM

people

How to return multiple objects in a method with c#?

I have a method which retrieves multiple results from my database, I want to return each of these results as an object back to my page however currently it is just returning the...
Jon Skeet
people
quotationmark

Basically, you should return a List<Personen> or some similar collection type. (Consider declaring the return type as IList<Personen> or IEnumerable<Personen> and using List<Personen> as the implementation... more 6/12/2014 9:49:44 AM

people

warning appears when using generic types and inheritance

I created an abstract class of general type variables which other type (boolean, char, int) classes extend. I made a static method that creates these variables according to a...
Jon Skeet
people
quotationmark

I suspect you just need to change the return type of the method: public static Variable<?> createVariable(String type) That basically says, "I'm returning a variable of some type, but I have no information about what the type... more 6/12/2014 9:18:12 AM

people