Browsing 7239 questions and answers with Jon Skeet

Casting contents of an indexer's returned collection?

I have a table/row/column data structure setup. There is a string-based indexer in the DtaTable class to return DtaRows, and another on the DtaRow class to return DtaColumns. So...
Jon Skeet
people
quotationmark

It you know that callers are only primarily going to use another indexer, you can introduce a generic class providing that: public class SpecializedTable<T> { private readonly DtaTable table; // Just in case anyone really... more 9/24/2015 2:25:36 PM

people

Apparently (!) inconsistent signing between .NET and Mono; Mono signing is not idempotent

Google Cloud Storage provides Java, C# code samples for generating signed URLs: https://cloud.google.com/storage/docs/access-control?hl=en#signing-code-csharp I'm using the code...
Jon Skeet
people
quotationmark

I've managed to get consistent results by not using CspParameters at all, but using the PrivateKey property of X509Certificate2. My current "portable" implementation unfortunately requires a cast which makes me nervous, but it does appear... more 9/24/2015 1:04:22 PM

people

order of evaluation of subexpressions in a Java expression

I have the following snippet of code: int x=2,y=3; if ( (y == x++) | (x < ++y) ) // rest of code I know that in C++ you're taught not to rely on evaluation order of...
Jon Skeet
people
quotationmark

Yes, it's guaranteed to be executed from left to right - or at least, act as if it is. This is defined in JLS 15.7: The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation... more 9/24/2015 7:28:19 AM

people

how to handle null exception in C#

im getting null exception . while im directly exceciting this page. i want to handle null exception C# string json = ""; if (Request.QueryString["data"] !="") { json =...
Jon Skeet
people
quotationmark

Well presumably Request.QueryString["data"] is null. You're currently checking whether it's a reference to an empty string, but not whether it's a null reference. I suspect you want to use string.IsNullOrEmpty to check that: string json =... more 9/24/2015 6:32:53 AM

people

When objects created with static reference, why do instance block & default constructor get executed before static block?

public class TestLab { static Test aStatic=new Test(); public static void main(String[] args) { TestLab obj=new TestLab(); } static{ ...
Jon Skeet
people
quotationmark

When the type is initialized, all the static initializers and all the static field initializers are executed, in textual order. From JLS 12.4.2: Next, execute either the class variable initializers and static initializers of the class,... more 9/24/2015 5:49:54 AM

people

Exception has been thrown by the target of an invocation (MethodBase.Invoke Method)

I want to catch the exceptions that are thrown in methods called with invoke method. public void TestMethod() { try { method.Invoke(commandHandler, new[] {...
Jon Skeet
people
quotationmark

Yes, you're calling the method via reflection. So as per the documentation, a TargetInvocationException will be thrown if the target method throws an exception. Just use the InnerException property to obtain - and potentially throw - the... more 9/23/2015 1:57:31 PM

people

How to use Union with different type of collections?

My method will need to return a collection that contains settings.AgentIds and agent ids that correspond to settings.LabelIds. So I decided to use Union...
Jon Skeet
people
quotationmark

If you're content to just flatten the IEnumerable<IEnumerable<Guid>> into an IEnumerable<Guid> in the obvious way, then SelectMany is your friend: IEnumerable<Guid> labelAgentIds = labelGroups.SelectMany(x =>... more 9/23/2015 11:52:49 AM

people

Preferable way to avoid if(condition) return true

I keep finding methods having code like this: public boolean checkSomethingForCollection(Collection<Something> things){ for(Something thing:things){ boolean...
Jon Skeet
people
quotationmark

Java streams in Java 8 make this pretty easy - the Stream.anyMatch method taking a predicate is exactly what you want. In this case you can use a method reference to create a predicate from the check() method. public boolean... more 9/23/2015 11:40:56 AM

people

No February 30th, 1712 in .NET's Swedish calendar?

At this wonderful talk about how weirdness in real life data reflects on computer data types, Jon Skeet tells that February had 30 days in Sweden in 1712 in a weird attempt to...
Jon Skeet
people
quotationmark

Basically .NET calendar code doesn't support cutovers between the Gregorian calendar and the Julian calendar... and even if it did, I wouldn't really expect it to support the oddities of the Swedish situation, which didn't follow either of... more 9/23/2015 10:39:38 AM

people

Casting to object in .NET reference source

I was going through the OperatingSystem.cs file in the .NET reference source and noted this code in line 50: if ((Object) version == null) version is an object of class...
Jon Skeet
people
quotationmark

No, it's not equivalent - because Version overloads the == operator. The snippet which casts the left operand to Object is equivalent to: if (Object.ReferenceEquals(version, null)) ... rather than calling the operator== implementation... more 9/23/2015 10:04:53 AM

people