Browsing 15836 questions and answers with Jon Skeet

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

How to get input and return type of delegate stored in list<dynamic> in c#?

I want to create list of method. and run this list of method in order. The input of next method is output of current method. So, this is my code for these, and i need to get...
Jon Skeet
people
quotationmark

Yes, you can use foreach, and call Invoke dynamically - you'll need the input to be dynamic as well though: using System; using System.Collections.Generic; class Test { static void Main() { Func<int,int> fn1 = new... more 8/7/2017 7:12:29 AM

people

How to work around rank N polymorphism in C#?

Suppose there is an identity function, which is: T Id<T>(T t) { return t; } What should I type F? void F<T>(Func<T, T> f) { // This is not sound! ...
Jon Skeet
people
quotationmark

It's not clear to me what you're trying to achieve, but you could do this with a non-generic interface that contains a generic method: using System; interface IGenericSameTypeFunction { T Apply<T>(T input); } public class... more 8/6/2017 8:37:03 AM

people

Primitive type casting in java (disconcerting examples)

I've read most of the threads on this site concerning typecasting (please do not link me to any more or mark this as a duplicate), and would like an explanation on some of these...
Jon Skeet
people
quotationmark

The first two are handled by the same case - JLS section 5.2: In addition, if the expression is a constant expression (ยง15.28) of type byte, short, char, or int: A narrowing primitive conversion may be used if the type of the... more 8/6/2017 6:46:13 AM

people

Why AsEnumerable also execute filter on server

I'm using Linq to Entities in my program, and i have the following two simple queries: var result = dbContext.Customers.Where(c => c.Name == "Mostafa").ToList(); var result2 =...
Jon Skeet
people
quotationmark

AsEnumerable() makes the remainder of the query execute locally. Anything earlier than the AsEnumerable() is still part of the IQueryable execution flow. For example, think about this (imagining an Age property): var result =... more 8/5/2017 10:00:24 PM

people

C# ; expected in Throw New Excepction error using Assert

Im using this code in C# project public async Task should_not_raise_exception(Mock<IWebSocketClient> webSocket, SlackConnection slackConnection) { // given ...
Jon Skeet
people
quotationmark

You can reproduce the problem with just this line of code: Action<string> foo = message => throw new Exception(); The problem is that before C# 7.0, you couldn't use throw as an expression on its own... and that's what an... more 8/5/2017 9:00:35 PM

people