Browsing 7239 questions and answers with Jon Skeet

Shadowing instance variables with local variables in Java

I have read that " a variable is shadowed if there is another variable with the same name that is closer in scope". I found this Point class with a constructor as an...
Jon Skeet
people
quotationmark

I thought that "x = x" in the constructor would be like "23 = 23". Within the constructor, the meaning of the simple name x is always just the parameter. So the assignment x = x in the constructor takes the value of the x parameter... more 5/2/2014 4:24:57 PM

people

cannot make publicly visible class from internal class?

I have an internal class: internal abstract class Reader { //internal abstract void methods } I cannot make this class: public abstract class CustomFileReader :...
Jon Skeet
people
quotationmark

You can't have an internal class in the inheritance tree of a public class, but you can force users to derive from CustomFileReader instead of Reader, by making the only constructor of Reader internal: public abstract class Reader { ... more 5/2/2014 4:21:55 PM

people

Use Action or FUNC in dictionaries

i was wondering if it is possible to pass a template into a function like this example: Dictionary<string, Action<object>> myDict= new Dictionary<string,...
Jon Skeet
people
quotationmark

this method for example is a setter so it receives as a parameter (double or string or int etc...) and return void. That's the problem - a method accepting a double isn't applicable for Action<object>, because an... more 5/2/2014 12:43:33 PM

people

Abstract Servlet is this method threadsafe?

I've read a lot of about servlets and threadsafe - I know, that "Servlet container loads and instantiates each Servlet only once...". But if I create abstract class extends...
Jon Skeet
people
quotationmark

You haven't shown anything in the getNameViaParameter method that affects any shared state within the servlet, so it should be fine. You won't end up with multiple calls for the same request in different threads, so as long as your method... more 5/2/2014 12:30:42 PM

people

Reflection check for parent class

How do I check if class inherits from my class DataSource (abstract class). here is what I got: var q = from t in Assembly.Load(new...
Jon Skeet
people
quotationmark

It sounds like you just want: var query = Assembly.Load(...) .GetTypes() .Where(t => typeof(DataSource).IsAssignableFrom(t)); (The IsAssignableFrom part is the interesting bit, but I gave the... more 5/2/2014 12:08:50 PM

people

Generic function creates generic object of arguments base

I have the following code: public class Foo { public void DoSomething() { DoSomething(this); } private static void DoSomething<T>(T obj) { ...
Jon Skeet
people
quotationmark

The compile-time type of this within Foo.DoSomething is just Foo, so the compiler can only infer the type argument as Foo. The simplest way of getting it to do it based on the execution-time type is probably: DoSomething((dynamic)... more 5/2/2014 12:07:12 PM

people

Getting a dictionary by ref

I was wondering if a right value of a dictionary can be by ref. so, when i'm changing the value of the dictionary some other value will also will change for example: double num =...
Jon Skeet
people
quotationmark

No, that's basically not possible. If you want something like that, you'd need a wrapper class type, e.g. public class Wrapper<T> { public T Value { get; set; } } Then you could use: var wrapper = new Wrapper<double> {... more 5/2/2014 11:23:42 AM

people

How to search the same object with different Equal concepts?

I have several objects that depending on the use case are considered Equal differently. I need to use these objects as keys for dictionaries and as far as I know...
Jon Skeet
people
quotationmark

Any one dictionary can only have a single equality comparer. You can't ask it to find a key with respect to a particular equality comparer, because otherwise its stored hash codes will be useless, and it would have to just do a linear... more 5/2/2014 10:57:58 AM

people

How to check if a List's value is contained within another List?

Hi guys i'm relatively new to LINQ so not the greatest of knowledge but i'm pretty sure this can be done using it. I have two lists, a list of propertyAppliances and a list of...
Jon Skeet
people
quotationmark

If you only want to know whether there are any, I'd use: if (propertyAppliances.Select(pa => pa.Appliance.TypeId) .Except(engineers.Select(eng => eng.ApplianceTypeId)) .Any()) This... more 5/2/2014 10:53:27 AM

people

Foreach statement cannot operate on type ' ' because it does not contain a public definition for 'GetEnumerator'

I have the following in my Api Controller: [AcceptVerbs("POST")] public Model.ViewModel.ContactSaveRequest DeleteMethod(Model.ViewModel.ContactSaveRequest methodsToDelete) { ...
Jon Skeet
people
quotationmark

It sounds like you just want to use: foreach (var contactMethod in methodsToDelete.ContactMethods) You can't iterate over a ContactSaveRequest, but you can iterate over the IEnumerable<ContactMethod> that is returned by the... more 5/2/2014 10:26:55 AM

people