Browsing 7239 questions and answers with Jon Skeet

C# Store a generic list parameter as a instance scope variable

I have the following method: public void SetList<T>(IList<T> listINeedToStore) { //Store the list in instance level scope } I would like to take the...
Jon Skeet
people
quotationmark

You can't do it in a generic way without making the type generic instead of the method, basically. You can store the reference, but it would have to be via a field of a non-generic type, e.g. IEnumerable or even just object. To really use... more 12/9/2013 6:46:06 AM

people

Can IEnumerable.Select() skip an item?

I have this function: public IEnumerable<string> EnumPrograms() { return dev.AudioSessionManager2.Sessions.AsEnumerable() .Where(s => s.GetProcessID != 0) ...
Jon Skeet
people
quotationmark

No, Select always yields one output element for each input element. There's no alternative to that. You could easily write your own FilteredSelect extension method - but it's simpler just to use a Where clause. Alternatively, use... more 12/8/2013 1:11:15 PM

people

Calling method from two different classes that have an implement

ok so I have to make a consoleprint interface class and two other classes that implement it one class simply will let me print the text and the other u can see is string...
Jon Skeet
people
quotationmark

You need to move your method call into your main method (or at least some method). Currently it's not in a method - only declarations (of fields, methods, nested types etc) can be at the top level of a class. Additionally, you're not... more 12/8/2013 1:01:22 PM

people

ArgumentOutOfRangeException. But it should not be there

So I have and method like this. var someColletion = _someService.GetSomeCollection(someParam); var taskCollection = new Task<double>[someCollection.Count]; for (int i =...
Jon Skeet
people
quotationmark

I suspect you're using i within the first loop, capturing it with a lambda expression or anonymous method, like this: for (int i = 0; i < taskCollection.Length; i++) { taskCollection[i] = Task.Run(() =>... more 12/7/2013 8:34:26 AM

people

How to specify an object that is of one class but not a certain subclass?

class A { } class B : A { } void method(A that is not a B argument) {} void generic_method(generic_class<A that is not a B> generic_argument) {} void...
Jon Skeet
people
quotationmark

So it sounds like you're trying to express the opposite of a constraint such as: var Foo<T>() where T : SomeClass That would constrain T to be SomeClass or a subclass1... but you're trying to make it explicitly not a T. No, I'm... more 12/6/2013 11:01:00 PM

people

What's the difference in memory between creating an object inside and outside of a loop

I want to clear up a few gaps in my knowledge. Starting with this. Example (c#): List<Person> names = new List<Person>(); Person friend = null; for(int i = 0; i...
Jon Skeet
people
quotationmark

It is my understanding that each time I instantiate friend, I reuse the same location in memory, overwriting the existing Person object, if present. No, that's not the case. You're overwriting the previous value in the variable - but... more 12/6/2013 10:42:42 PM

people

C# Overload method with var type

I am querying a SQL database via LINQ in my C# project and returning the data to a var type variable How can I overload a method with var type? example of what I am trying to...
Jon Skeet
people
quotationmark

I am querying a SQL database via LINQ in my C# project and returning the data to a var type variable The type of the variable isn't var - that's just the form of declaration you're using. The type of the variable is whatever the... more 12/6/2013 8:36:04 PM

people

Select node with xpath

I want to go to a deep link for the xml with xpath, and also there is a name space in it. Currently I have: XmlNode link = xmlDoc .SelectSingleNode("dn:feed", nsmgr) ...
Jon Skeet
people
quotationmark

I suspect this is a namespace problem. You're only specifying the namespace for the feed element, not for entry or link. Try this: XmlNode link = xmlDoc.SelectSingleNode("dn:feed/dn:entry/dn:link", nsmgr); Personally I'd use LINQ to XML... more 12/6/2013 6:38:00 PM

people

C# Image Class to Byte Array Length Issues

I use the Image class of C# to read in a file: var image = Image.FromFile(filePath); this now means image has been filled with Image data. I now convert this image to a byte...
Jon Skeet
people
quotationmark

You seem to be assuming that imageIn.Save(ms, ImageFormat.Bmp); will be writing exactly one byte per pixel. In fact, it's storing it in BMP format - which may well be more than one byte per pixel due to headers, and the fact that you... more 12/6/2013 5:02:55 PM

people

NullReferenceException at System.Collections.Generic.LinkedList`1.AddLast(T value)

I have a LinkedList<T> in my project where I add and remove a lot of elements (few hundred per second). This happens from multiple threads synchronized via locks. Now...
Jon Skeet
people
quotationmark

This happens from multiple threads synchronized via locks. I suspect you forgot to lock in one place, basically. Is that possible, or are you certain you only access it within a single place? For example, I can see how you'd get that... more 12/6/2013 3:05:14 PM

people