Browsing 15528 questions and answers with Jon Skeet

Empty catch but exception is still thrown

Have a look at the following source code private IEnumerable<string> GetAntivirusSoftwareFromNamespace(string @namespace) { try { var...
Jon Skeet
people
quotationmark

The exception isn't thrown within the method you've shown - you're basically returning a query to the caller, and that query is then failing. One option would be to force the query to execute immediately by calling ToList(). That way, any... more 1/22/2017 5:48:29 PM

people

How to convert a string into Int collection

Below is my string. var str = "1,2,3,4,5"; var strArr = str.split(","); // this gives me an array of type string List<int> intCol = new List<int>(); //I want...
Jon Skeet
people
quotationmark

Well that's a way of doing it, certainly - but LINQ makes it a lot easier, as this is precisely the kind of thing it's designed for. You want a pipeline that: Splits the original string into substrings Converts each string into an... more 1/22/2017 9:28:24 AM

people

i am getting an error Argument 2 may not be passed with ref keyword while using ado.net

int? t = 0; cmd.Parameters.AddWithValue("@Res", ref t); I get an error in the second line: argument 2 may not be passed with ref keyword.
Jon Skeet
people
quotationmark

You can only pass an argument by reference with ref if the parameter is a ref parameter as well. AddWithValue doesn't have any ref parameters, so you can't use it that way. Note that you have to specify ref when calling a method if a... more 1/21/2017 9:33:46 AM

people

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