Browsing 7239 questions and answers with Jon Skeet

Java Incrementing a variable before sending to the method

Situation Well, this method is managing a conversion which accepts a list as a parameter, but definately doesn't look scalable. List<Long> array_list_data= new...
Jon Skeet
people
quotationmark

k++ performs a post-increment. In other words, the value of the expression is the original value of k, and then k is incremented. It's still incremented before the method is called, but the value passed as the argument is the value before... more 7/31/2015 12:37:20 PM

people

Why is this cast redundant?

I have a method with the following overloads: string Call(string function, Dictionary<string, object> parameters, object body) string Call(string function,...
Jon Skeet
people
quotationmark

But why isn't my call ambiguous without the cast? Because the overload with the JObject parameter is "better" than the overload with the object parameter... because the conversion from null to JObject is "better" than the conversion... more 7/31/2015 11:46:35 AM

people

C# project folder namespace with ' ' character

I realized that folder containing a '-' character which are namespace provider do get a different namespace than expected. e.g. I observe that the embedded...
Jon Skeet
people
quotationmark

Hyphens aren't allowed in identifiers, and namespaces are identifiers. The reason is pretty simple, if you think about it - x-y is the binary - operator with operands x and y. Trying to make it also a valid identifier would be pretty... more 7/31/2015 8:36:53 AM

people

Adding Dictionary to a value of another Dictionary

I have the following scenario :: public static class ServiceErrorCode { public static IDictionary<string,Dictionary<int,string>> Mappings { get; ...
Jon Skeet
people
quotationmark

The Add method returns void - so you can't use it as a method argument. However, you can use a collection initializer: Mappings.Add("Unauthorized", new Dictionary<int,string> { { 103, "Test" } }); That's roughly equivalent... more 7/31/2015 7:28:40 AM

people

File Parse code gives me exceptions instead of a number and file writing code gives writes gibberish instead of a number

This code reads a notepad file this notepad file has the number 10 on it it returns a gibberish letter for some reason instead of 10 I think it is the ascii code but i do not know...
Jon Skeet
people
quotationmark

This line doesn't do what you expect: output.write(number); It's calling write on a BufferedWriter, so you should consult the documentation... at which point you find you're calling this method. public void write(int c) throws... more 7/30/2015 5:16:01 PM

people

.NET Do lambdas prevent garbage collection of external references used in them?

Here is an example: var task = Task.Run(); var func = () => task.Result; So if I loose the task reference and keep the func reference is GC going to collect the task and...
Jon Skeet
people
quotationmark

No. An anonymous function captures the variable, extending its lifetime to at least when the delegate or expression tree is garbage collected. From the C# 5 spec, section 7.15.5.1: When an outer variable is referenced by an anonymous... more 7/30/2015 4:53:28 PM

people

Getting the type of the object inside List inside ItemsSource

I have a DataGrid that is given a List, which can be either of type Foo, Bar, or Baz. Later on, I need to extract that data to save it, and to do so I need to know the type of the...
Jon Skeet
people
quotationmark

You're mixing two things - is to check whether an object is of the given type, and GetType() which returns the Type reference. The type of DataGridType is Type, and a Type object is never an instance of List<Foo>. (Imagine casting... more 7/30/2015 2:57:48 PM

people

How to call a field of child class in the method of parent class in C#

I want to print the salary of invoked (any of them) child class in the inherited method of Detail(). this cannot be done in this scenario. How would I get the salary with all...
Jon Skeet
people
quotationmark

You can't and shouldn't - there's no guarantee that an Employee will have a salary. Instead, any class which does have a salary can override ToString to include all the properties it wants to. I'd suggest overriding ToString instead of... more 7/30/2015 1:44:04 PM

people

How to compare 2 similar types in C#

I want to ask how do we compare two types in C#. My Scenario is: Nullable<int> Rating; int NeedCompareType; Every time I compare these two, it will return false...
Jon Skeet
people
quotationmark

Actually, this line: if(Rating.GetType() == NeedCompareType.GetType()) would always either go into the condition, or throw a NullReferenceException - because Rating.GetType() will box Rating to either a boxed Int32 or a null... more 7/30/2015 10:49:43 AM

people

Skipping element in a for loop and reassigning it

I'm filling parameters in JasperSoft. In my report I have the Parameters: Parameter_1, Parameter_2, Parameter_3 int a; for (a = 0; a < headers.length; a++) { ...
Jon Skeet
people
quotationmark

It seems to me that you just need to keep a separate index for the "next parameter to put": int parameterIndex = 1; // Note: more idiomatic to declare the iteration variable // inside the loop for (int headerIndex = 0; headerIndex <... more 7/30/2015 8:56:14 AM

people