Browsing 7239 questions and answers with Jon Skeet

Func<> with lambda expression

I have found following part of code in some examples while learning Func< syntax: public static class Lambda { public static int MyFunc(Func<string, int>...
Jon Skeet
people
quotationmark

url is the name of the parameter for the lambda expression. It's like writing a method like this: public static int Foo(string url) { Console.WriteLine(url); return 0; } Then creating a delegate from it: Func<string, int>... more 7/20/2015 9:20:36 PM

people

C#: Extension Methods not accessible with aliased using directive

The following code compiles: using Microsoft.SharePoint.Client class Dummy() { void DummyFunction(ClientContext ctx, ListCollection lists) { Context.Load(lists,...
Jon Skeet
people
quotationmark

Not before C# 6... but in C# 6 you can use: using static Microsoft.SharePoint.Client.ClientObjectQueryableExtension; using SP = Microsoft.SharePoint.Client; The first of these will pull in just the static members of... more 7/20/2015 8:28:02 PM

people

Typecasting an object in C#

Assume I have a method which returns object of class A A getItem(int index) Now I have following line of code, (I assume B is subclass of A) B b = (B) obj.getItem(i); but...
Jon Skeet
people
quotationmark

Two options: object item = obj.getItem(i); // TODO: Fix method naming... // Note: redundancy of check/cast if (item is B) { B b = (B) item; // Use b } Or: object item = obj.getItem(i); // TODO: Fix method naming... B b = item... more 7/20/2015 7:15:22 PM

people

How to parse json array with Newtonsoft.Json?

Please help me to parse array from this json in C#: { "error_code":0, "response": { "17": { ...
Jon Skeet
people
quotationmark

That's not a JSON array - it's just a JSON object which happens to have numbers for the properties of the response object. You can parse it as a JObject, or deserialize it to a class like this: public class Root { public int... more 7/20/2015 1:01:56 PM

people

Operation to "Get and AND" boolean variable in Java

Is it possible in Java to use syntax like (i++, ++i) for boolean logic operators? I have a boolean variable that is true only for the first iteration of a foreach loop. That...
Jon Skeet
people
quotationmark

Personally I would simplify your current code to: for (...) { if (bool) { bool = false; continue; } // Rest of code } ... but if you really want to do it in the if condition as a side-effect, you could... more 7/20/2015 10:29:56 AM

people

Async method to return true or false in a Task

I know that an async method can only return void or Task. I have read similar methods for Exception handling inside async methods. And I'm new to async programming so I am looking...
Jon Skeet
people
quotationmark

It sounds like you just need to return a Task<bool> then: private async Task<bool> RefreshContactsAsync() { try { ... } catch // TODO: Catch more specific exceptions { return false; } ... more 7/20/2015 9:53:06 AM

people

SQL Output Parameter

I have a questionnaire which stores answers for each user who does the test. I need to store the output in the database (which is the user doing the test.) I'm not quite sure what...
Jon Skeet
people
quotationmark

This is at least part of the problem: cmd.Parameters.Add("@InputID", SqlDbType.Int).Value = ParameterDirection.Output; That's creating an input parameter - but giving it a value which is the enum value ParameterDirection.Output. You... more 7/20/2015 6:10:39 AM

people

Expression.Lambda variable '' of type 'System.String' referenced from scope '', but it is not defined

I am trying to build a system that loads function delegates to a dictionary and then they can be called from anywhere in the environment asking the dictionary for the...
Jon Skeet
people
quotationmark

You're calling Expression.Parameter twice, which gives you different expressions. Don't do that - just call it once, and use that ParameterExpression both places you need it: var parameter =... more 7/19/2015 8:27:09 AM

people

Why Volatile doesn't work when the value of a field depends on its previous value

I came across this statement, for which I could not think of a reason. Volatile doesn't work when the value of a field depends on its previous value Any explanation with...
Jon Skeet
people
quotationmark

"Doesn't work" is a pretty vague claim, but I suspect that the point is that if you have two threads each executing a loop with this code: field = field + 1 ... then even when it's volatile, each iteration is effectively: Read Perform... more 7/19/2015 7:28:49 AM

people

Do elements in LinkedList class of java have index?

Do elements in LinkedList class of java have index? If yes then why is its performance o(n) in worst case for search operation when it can directly search using index of the...
Jon Skeet
people
quotationmark

They have a logical index, yes - effectively the number of times you need to iterate, starting from the head, before getting to that node. That's not the same as saying "it can directly search using index of the object" - because it... more 7/19/2015 7:14:17 AM

people