Browsing 7239 questions and answers with Jon Skeet

Adding xml:space to root element

I have a little problem that I thought was a no-brainer ... but alas ... I have some xml and all I want to do is to add the xml:space="preserve" to the root element using c#. I...
Jon Skeet
people
quotationmark

You just need the right XName value - I'd use this: doc.Root.SetAttributeValue(XNamespace.Xml + "space", "preserve"); The XName +(XNamespace, string) operator is generally the simplest way to work with namespaces in LINQ to XML, in my... more 3/7/2016 2:32:44 PM

people

C# Winforms Log In Form using Binary File

I am currently working on a school project that will have your classic log-in/register form. The project requires that we include a binary file to 'securely' store each User's...
Jon Skeet
people
quotationmark

This is the problem: if ((newUser.Username == a) && (newUser.Password == b)) okFlag = true; else okFlag = false; That code is run for every entry in the file. So if the okFlag is set to true from the first entry, but... more 3/6/2016 7:23:07 PM

people

Why can you still use a disposed object?

I'm battling a bit to understand disposing of objects and garbage collection. In particular, I don't understand why I can still use a disposed object. I'm not trying to do...
Jon Skeet
people
quotationmark

Disposing of an object doesn't do anything magical - the CLR doesn't really care about IDisposable at all... it's just a framework interface that has support within C# (and other languages). Calling Dispose is just like calling other... more 3/6/2016 9:42:20 AM

people

Adding a new XML node to a last one of the same sort

I have an Excel table saved as a XML file, so there are some rows there. All I want is to add a new row to the last one using C#. Structure: <?mso-application...
Jon Skeet
people
quotationmark

It looks like you just need to find the relevant Table element, then append a new Row element to that. For example, using LINQ to XML, assuming there is only one Table element: var doc = ...; // XDocument.Load or whatever var table =... more 3/6/2016 9:28:41 AM

people

Why won't the shorthand version of my 'if' compile with different implementations?

I have the following interface: interface IMyInterface {} And the following two implementations: class MyImplementation : IMyInterface {} class MyOtherImplementation :...
Jon Skeet
people
quotationmark

What am I misunderstanding in assuming that it should compile? You're not reading the spec :) Basically, conditional operator requires that either the second and third operands are of the same type, or that there's an implicit... more 3/4/2016 1:28:50 PM

people

How to get time zone as a String in C# ?

I need to get time zone of PC as below format. (UTC+07:00) Bangkok, Hanoi, Jakarta How can I get it as a string ?
Jon Skeet
people
quotationmark

Just use the TimeZoneInfo.DisplayName property: var zone = TimeZoneInfo.Local; // For example Console.WriteLine(zone.DisplayName); Or for your precise example: var zone = TimeZoneInfo.FindSystemTimeZoneById("SE Asia Standard... more 3/4/2016 11:00:29 AM

people

Reflection can't get the actual parameter type of a method that overrides from a generic typed interface?

EDIT: Sorry the example I provide below doesn't reproduce the problem... I'm trying to find the right way to reproduce it. Before that, you may just ignore my question... Usually...
Jon Skeet
people
quotationmark

Yes, this is type erasure being annoying. It's worth looking at what you get in bytecode here: class Car {} interface IService<T> { void create(T obj); } class CarService implements IService<Car> { public void... more 3/4/2016 9:58:01 AM

people

Get common elements with index from two list C#

I have Two lists of type list<int> and i know we can find the common elements between two lists. But is there any way to get common elements and corresponding indexes of...
Jon Skeet
people
quotationmark

LINQ has operations to project a sequence using indexes, but this isn't built into the query expression syntax, so you have to use "regular" extension method calls to start with. After that it's fairly easy, although probably just as... more 3/4/2016 7:27:03 AM

people

Why is this:Interface<T> not of type Interface<T> using an IS keyword comparison?

I am trying to figure out why using the is keyword seems to differ when I cast. Here's a test case showing what I am looking at: [TestFixture] public class TestMy { public...
Jon Skeet
people
quotationmark

The type inference in Test1 means it's calling msgProcessor.CanProcess<RRMessage>() ... whereas in Test2 it's calling msgProcessor.CanProcess<IBaseMessage>() Now RRMessageProcessor doesn't implement... more 3/3/2016 11:22:15 PM

people

How do I write an array to a text file without overwriting it

This is the code i use to create the text file. System.IO.File.WriteAllLines(@"C:\Users\****\Desktop\File.txt", array); How do I append text to an existing file?
Jon Skeet
people
quotationmark

The other answers have shown you how to append a single string to a text file. If you naturally have a collection of lines, however, you probably want File.AppendAllLines: File.AppendAllLines(@"C:\Users\****\Desktop\File.txt", array); more 3/3/2016 7:09:18 PM

people