Browsing 15901 questions and answers with Jon Skeet

Convert c# bytes array function to vb.net

I am trying to convert this code public static byte[] NewLine(this byte[] bytes, int feeds = 1) { return bytes.AddBytes(((IEnumerable<byte>) new...
Jon Skeet
people
quotationmark

It's not clear to me why specifying the type arguments fails, but that isn't needed anyway - and the location of CType looks to be broken. If you change the Select call to: .Select(CType(Function(x) CByte(10), Func(Of Byte, Byte))) then... more 8/11/2017 11:33:32 AM

people

Why should one use Objects.requireNonNull()?

Why should one use Objects.requireNonNull() ? I have noted that many Java 8 methods in Oracle JDK use Objects.requireNonNull(), which internally throws NullPointerException if...
Jon Skeet
people
quotationmark

But NullPointerException will be thrown anyway if a null object is dereferenced. So, why should one do this extra null check and throw NullPointerException? It means you detect the problem immediately and reliably. Consider: The... more 8/11/2017 10:29:55 AM

people

Using named tuples in select statements

Is there a nicer way to select a named tuple in C# 7 using a var target variable? I must be doing something wrong in example 1, or misunderstanding something completely. I seem to...
Jon Skeet
people
quotationmark

You can just use var, but you need to make sure the tuple elements are actually named. In C# 7.0, you need to do this explicitly: var tuples = source.Select(x => (A: x.A, B: x.B)); foreach (var tuple in tuples) { ... more 8/11/2017 10:07:44 AM

people

Suppress "Use of unassigned local variable" error?

I have a method like so: public static long? FromIpv4ToLong(this string ipAddress) { var octets = ipAddress.Split(IpSplitChar); if (octets.Length != 4) return null; ...
Jon Skeet
people
quotationmark

Just refactor the code to avoid the pointless local variable that's confusing things, taking advantage of the fact that within the if body, the compiler does know that everything is definitely assigned: public static long?... more 8/10/2017 1:58:35 PM

people

LINQ deferred execution with a function's result as source (e.g. Console.ReadLine)

A function's result is the source for a LINQ query. I want it to be evaluated lazily, every time I use the query, not be locked when I create it. This is an example of what I...
Jon Skeet
people
quotationmark

If you don't mind a bit of extra infrastructure, it's not too bad - you can create a DeferredEnumerable<T> class that just executes the given delegate every time it's asked for an iterator. A static non-generic class can then help... more 8/10/2017 12:03:25 PM

people

What is the impact of C# closures on memory?

I have a class Test which holds two members, one (arr) which takes a lot of memory and the other (b) which doesn't: public class Test { public Test() { Arr = new...
Jon Skeet
people
quotationmark

Will it hold the whole Test object in its environment, or only Test.b? Well, it will capture the variable test (by creating a separate class to contain that variable), which in turn has a value which is a reference to the instance of... more 8/8/2017 3:58:37 PM

people

Dispose an object while a task is running async

Consider the following scenario... class FileProcessor : IDisposable { public void Dispose() { //Disposes resources. } public async void OnNext(string...
Jon Skeet
people
quotationmark

There's nothing magic about disposal. The Dispose method is called - if you're not affecting anything that the tasks use, that should be fine. Likewise setting fp to null just stops fp from being treated as a GC root... although unless... more 8/8/2017 8:41:08 AM

people

Google Cloud Platform pub/sub Publisher, how to supply credentials other than default application credentials

Using the com.google.cloud.google-cloud library (http://googlecloudplatform.github.io/google-cloud-java/0.21.1/index.html), I have the following code for the Google Cloud Platform...
Jon Skeet
people
quotationmark

You can set the credentials provider on the builder: GoogleCredentials credentials = GoogleCredentials.fromStream( new FileInputStream(PATH_TO_JSON_KEY))); Publisher pub = Publisher .defaultBuilder(topicName) ... more 8/8/2017 8:00:38 AM

people

Asserting for equality anonymous types

I faced a problem asserting two anonymous types. What I've done so far I have unit test project which sees the internals of my project under test, so anonymous types are...
Jon Skeet
people
quotationmark

Even though the anonymous types are accessible in your test project, that doesn't mean they'll be used when you write new { ... }. If you look at actionResult.Value.GetType() and expectedActionResult.Value.GetType() I strongly suspect... more 8/7/2017 8:32:27 AM

people

modify base class property in c#

i have two property in base class {ID, EncryptID} i have written this code in my base class you can see. public abstract class BaseObject { public int ID { get;...
Jon Skeet
people
quotationmark

We can't easily tell, but I strongly suspect that the problem is that LoadAll() returns a lazily-evaluated set of results. You call ToList() and modify those results - but then when you return Ok(result) you're then evaluating the results... more 8/7/2017 7:47:37 AM

people