Browsing 15485 questions and answers with Jon Skeet

How to correctly assign properties for other properties in JObject?

What I am trying to achieve is converting a JObject to XML Document and then extract the outer xml from the XMl Document. Reasons behind this is to send the results as a push...
Jon Skeet
people
quotationmark

Basically, don't use a tool oriented at JSON to construct XML. If you already had JSON, it would make sense to use Json.NET to convert it to XML - but as you're building it from scratch, it's much cleaner to use LINQ to XML: XDocument doc... more 1/20/2017 9:00:20 AM

people

Attribute value must be constant error using Contentful Space

Am getting the error message: Attribute value must be constant while defining a Space annotation for my Space class: I've looked at the question In Java why this error:...
Jon Skeet
people
quotationmark

If you look at the Javadoc for Space you'll see that the locales attribute is a String[] - so just provide the IDs of the locals: @Space(value = "id", dbVersion = 1, models = NewsVault.class, locales = { "en" }) more 1/20/2017 8:01:39 AM

people

Google Protocol Buffers serialize to byte array

I'm following the tutorial for using Google Protocol Buffers for C#. I don't see an example for converting an object into a byte array - does anyone know how to do that? I've...
Jon Skeet
people
quotationmark

Assuming you're using the Google.Protobuf nuget package, you can just use: using Google.Protobuf; ... byte[] bytes = fp.ToByteArray(); You need the using directive for Google.Protobuf to make the IMessage.ToByteArray extension method... more 1/19/2017 6:56:30 PM

people

Pass array to variable parameter method

I'm using JCommander in a project. Its documentation defines its parse method to be like parse public void parse(String... args) Parse and validate the command line...
Jon Skeet
people
quotationmark

That code will already work fine. The args parameter of parse is already of type String[] - the String... syntax just means that the compiler is allowed to convert this: parse("foo", "bar", "baz"); into parse(new String[] { "foo",... more 1/19/2017 8:42:35 AM

people

Using a class constructor to create object arrays of 2 different types

I'm implementing a database of sorts for a data structures project and i'm having a really hard time wrapping my head around the logic of what I need to do. I have an abstract...
Jon Skeet
people
quotationmark

Firstly, I'd question the approach - it sounds like this single class is trying to do too many things. But if you really, really want to do it, I'd recommend static factory methods calling a private constructor: public class... more 1/19/2017 7:04:40 AM

people

Call extension method created on parent class overloaded in child class

I am having code as below where WebServiceSender is the parent class. I have added an Extension method to this as "ExtTest". I have one child class "ChildWebServiceSender"...
Jon Skeet
people
quotationmark

How can i call Extension method ExtTest ? Instance methods will always be preferred over extension methods - the compiler only checks for extension methods after everything else has failed. In this case, the simplest approach is just... more 1/17/2017 3:26:51 PM

people

IOException while calculating MD5 and SHA1 checksums

I have a problem while executing a method that computes MD5 and SHA1 checksums for a given file. My methods look like this: SHA1: public string HashFile(string toHash) ...
Jon Skeet
people
quotationmark

You're opening the file without closing it. Just open the stream separately so you can close it in a using statement: MD5 md5 = MD5.Create(); using (var stream = File.Open(...)) { var hash = md5.ComputeHash(stream); //... more 1/17/2017 11:38:59 AM

people

Search For keys Matching values in a dictionary of list in C#

I have a dictionary of lists. var dicAclWithCommonDsEffectivity = new Dictionary<string, List<int>>(); var list1 = new List<int>(){1,2,3}; var list2 = new...
Jon Skeet
people
quotationmark

Is there a way without looping through each item in the list of dictionary value. Something has to loop - dictionaries are only designed to look up by key, and you're not doing that other than for the first check. You can do this... more 1/17/2017 7:16:43 AM

people

Custom Numeric Format Negative section bug

Not sure that it is 100% bug but behavior really strange. My task is properly format fraction. For example if : int numerator = -7; int denominator = 100; Then if next line...
Jon Skeet
people
quotationmark

Your format string is "#/100;-#/100;0". That's a very strange format string. You're saying: If the number is positive, use a format of "#/100". If the number is negative, use a format of "-#/100". If the number is zero, use a format of... more 1/16/2017 9:41:08 PM

people

Initialization with null coalescing operator?

I may not have a good grasp of the ?? operator yet and ran into a design flaw I couldn't explain. Compare the following two properties, the only difference being how there are...
Jon Skeet
people
quotationmark

Look at what this line does: get { return _userNameColl?? new ObservableCollection<UserName>(); } If the field value is non-null, it just returns it. Great. Any changes made will affect the object that the field refers to. If the... more 1/16/2017 11:29:53 AM

people