Browsing 7239 questions and answers with Jon Skeet

Reading from a .txt while inside a class

I'm getting an error is a field but is used as a type. I've looked around here and tried putting it outside of the class like private string[] patron = new...
Jon Skeet
people
quotationmark

This is the problem: new line.Split(':'); That makes it look like you're trying to create an instance of a type called line (although without specifying any arguments). You just want: private string[] patron = line.Split(':'); ...... more 7/17/2015 7:35:02 AM

people

Whether the object can be collected by GC

Hi all: assuming I got the Class A and B: class A { public string foo = "foo"; public int a = 10; public double d = 3.14; } class B { public string s; } A a =...
Jon Skeet
people
quotationmark

My question is after a is set to null, whether it can be collected by GC at next GC trigger point even though b reference one of its fields, foo? The value of b.s happens to have been set to a.foo, but that's just copying the value.... more 7/17/2015 7:16:53 AM

people

Pass Type as parameter and check

I want to build a method that accepts parameter as Type like void M1(Type t) { // check which type it is } and call it like M1(typeof(int)); I have no idea how to check...
Jon Skeet
people
quotationmark

If you want to check for an exact type, you can use: if (t == typeof(double)) That's fine for double, given that it's a struct, so can't be inherited from. If you want to perform a more is-like check - e.g. to check whether a type is... more 7/17/2015 5:58:39 AM

people

Execute Function in IQueryable

Using Linq, it is easy to execute a function to project an IEnumerable. var orders = new List<Order>(); orders.Where(x => x.Id > 50).Select(x => new...
Jon Skeet
people
quotationmark

No, because you'd need to ship all the data back to the database to do the final part. Typically you have an extra Select to get just the information you need within the query (using an anonymous type if there are multiple properties you... more 7/17/2015 5:29:29 AM

people

An object's child object is null when passed in argument

I have the following object structure: class Facts { string name; string color; //other stuff } class Fruit { Facts Facts {get; set;} } class Apple : Fruit { Facts Facts...
Jon Skeet
people
quotationmark

The problem is that you've got two entirely independent properties called Facts for an Apple. That's a really bad idea - it makes this sort of thing really confusing. The compiler is using whichever one it has access to, based on the... more 7/16/2015 7:36:43 PM

people

Convert a for loop to concat String into a lambda expression

I have the following for loop which iterates through a list of strings and stores the first character of each word in a StringBuilder. I would like to know how can I transform...
Jon Skeet
people
quotationmark

Assuming you call toString() on the StringBuilder afterwards, I think you're just looking for Collectors.joining(), after mapping each string to a single-character substring: String result = list .stream() .map(s ->... more 7/16/2015 2:22:11 PM

people

The type arguments cannot be inferred from the query in linq c# query

Below is my piece of linq code mTimeslot = (from users in mContext.MFUsers join tblTime in mContext.tblTimeslots on users.UserID equals tblTime.varAdvisorId ...
Jon Skeet
people
quotationmark

tblTime.varSlotId is an integer where tblApp.varSlotId is a nullable integer. Well that's the problem then. Your two anonymous types aren't the same, and they need to be. It's probably simplest just to cast tblTime.varSlotId: join... more 7/16/2015 10:31:55 AM

people

How can I this fix covariance issue with Action on .NET 2?

I need to create a covariant interface with a method that takes a delegate with covariant generic parameter. Here's the code sample under question: interface IExample<out...
Jon Skeet
people
quotationmark

Why does this error occur in early .Net versions? Because Action wasn't contravariant in .NET 2.0 (or 3.5). How can I fix it? Don't use .NET 2.0 :) I thought that modern versions of Unity were based on more recent versions of... more 7/16/2015 10:27:23 AM

people

passing a list of Types generically for use in a generic method

Given the list of types: public class TypeLists { public static List<Type> types { get { return new List<Type>() {...
Jon Skeet
people
quotationmark

I'm surprised at the nature of the error method you're getting, but not the fact that you're getting an error in the first place. You can't specify a generic argument as an expression - the aim is for it to be a compile-time specification... more 7/16/2015 6:25:01 AM

people

How to use cast, DATEADD, DATEDIFF in linq query?

I am using a LINQ query in a C# application. My SQL query looks like this : select cast(DATEADD(hh, -4, [firstdate]) as DATE) as [Date], COUNT(Name) as 'Total Name', from...
Jon Skeet
people
quotationmark

You can use the SqlFunctions class which has DateAdd and DateDiff methods, effectively as proxies to the SQL code. Your LINQ to SQL could probably call DateAdd just the once though - if you select it, you can then group by the result of... more 7/16/2015 6:16:25 AM

people