Browsing 7239 questions and answers with Jon Skeet

C# Can I use an array initializer to build one byte array out of another?

I'd like to use an array initializer to build one byte array out of another byte array as well as some other bytes that form a header/trailer. Basically, I'd like to do something...
Jon Skeet
people
quotationmark

The closest you could get would be: byte[] DecorateByteArray(byte[] payload) => new byte[] { 0, 1, 2 } .Concat(payload) .Concat(new byte[] { 3, 4, 5 }) .ToArray(); That would be pretty inefficient though.... more 8/24/2016 3:30:46 PM

people

The read from a TCP Server hangs when reading more than once

I am testing some TCP code and it seems to work fine except for one problem. The read from the socket hangs in one of the methods when there is nothing more to read: Here is the...
Jon Skeet
people
quotationmark

You're continuing to try to read from the server until it's closed the socket - whereas the server is waiting for another command from the client. Neither side is going to do anything, as they're waiting for the other. Basically, you need... more 8/24/2016 3:04:00 PM

people

NumberFormatException for String while parsing float

This error is probably caused due to my different system locale. I use DOT as decimal separator. I tried to set a default locale when my application starts but I'm getting the...
Jon Skeet
people
quotationmark

It looks like your code is using Float.parseFloat. That doesn't use any locale settings. From the docs: Returns a new float initialized to the value represented by the specified String, as performed by the valueOf method of class... more 8/24/2016 6:22:04 AM

people

How is this call to method resolved in Inheritance in java?

Why doAction(A a) will be selected in this situation? Can you advice some articles to read about method selection depending on argument type? class A { } class B extends A { }...
Jon Skeet
people
quotationmark

Why doAction(A) will be selected in this situation? Because it's the only applicable method. Overload resolution is performed at compile-time, based on the compile-time type of the arguments. The doAction(B) method isn't applicable,... more 8/23/2016 11:43:35 AM

people

C# LINQ Orderby How does true/false affect orderby?

I was studying a bit of LINQ ordering as I have a list of Ids, and I need to order them sequentially. However, there are certain ids that need to take precedence over the standard...
Jon Skeet
people
quotationmark

Basically, false is earlier than true... think of them as false=0, true=1. This is in-keeping with the documentation for bool.CompareTo(bool). If you want to prioritize "true" values to the start, just use OrderByDescending instead. more 8/23/2016 6:37:22 AM

people

Why can't PowerShell find the gcloud cmdlets?

I've updated the Google Cloud SDK (gcloud), both in general with: gcloud components update And just for PowerShell with: gcloud components update powershell Running gcloud...
Jon Skeet
people
quotationmark

Unfortunately the PSModulePath modification required to make the cmdlets available to PowerShell only happens when the Google Cloud SDK is installed - not when it's just updated. The simplest fix for most people will be to uninstall and... more 8/22/2016 4:51:48 PM

people

Why doesn't my Gmail user/password work when deploying ASP.NET to Compute Engine inside Visual Studio?

I created a Compute Engine VM using the ASP.NET Cloud Launcher, and now I want to deploy to it from Visual Studio. I've created a publish settings file using the Visual Studio...
Jon Skeet
people
quotationmark

The Compute Engine instance doesn't know about your Google user at all - it only knows about regular Windows accounts, and you don't have a Windows account on it. So, you need to create a Windows account on the instance, and then put that... more 8/22/2016 11:12:07 AM

people

C#6 null conditional operator as a code toggler?

I already know that the null-conditional operator can be used to test for null before performing a member access. for example : int? length = customers?.Length; // null if...
Jon Skeet
people
quotationmark

The null conditional operator is an operator which results in an expression - it's not a statement. Your logic about Foo?.Invoke(this, EventArgs.Empty) being equivalent to null doesn't really hold, as it seems to assume a sort of... more 8/22/2016 8:23:18 AM

people

Typeof not working as condition

I currently have a method to see what type of object an input is and create a SQL input based on it, Like so: private static string PropertyToDBString(object o) { ...
Jon Skeet
people
quotationmark

I currently have a method to see what type of object an input is and create a SQL input based on it Don't do that. Use parameterized SQL instead. You should not be trying to format your values for use in SQL. This is almost always... more 8/21/2016 7:00:02 AM

people

Linq All on empty collection

I need to check if all definitions contains some specific data. It works fine except the case when GroupBy returns empty collection. var exist = dbContext.Definitions ...
Jon Skeet
people
quotationmark

If you're using LINQ to Objects, I'd just write my own extension method. My Edulinq project has sample code for All, and adapting that is pretty simple: public static bool AnyAndAll<TSource>( this IEnumerable<TSource>... more 8/19/2016 1:09:23 PM

people