Browsing 7239 questions and answers with Jon Skeet

Is it possible to define a "not Nullable<T>" constraint in a C# generic method?

In C#, the Nullable<T> type does not satisfy the where struct generic constraint (while AFAK this is techically a struct). This can be used to specify that the generic...
Jon Skeet
people
quotationmark

As noted in a comment, you can do this with overloads and parameters (which can be optional). I blogged about this a while ago, but in your case you'd want: public class ClassConstraint<T> where T : class { } public class... more 2/11/2015 11:33:01 AM

people

Calling a method when another method is called

This might be a stupid question, but here goes. I have the following problem: public class MyBaseClass { public void SomethingAwesome() { //Awesome stuff happens...
Jon Skeet
people
quotationmark

It sounds like you want the template method pattern: public abstract class MyBaseClass { public void Something() { // Code from SomethingAwesome here, or keep SomethingAwesome // separate and call it from here ... more 2/11/2015 9:32:12 AM

people

Does the compiler discard empty methods?

Would C# compiler optimize empty void methods away? Something like private void DoNothing() { } As essentially, no code is run aside from adding DoNothing to the call stack and...
Jon Skeet
people
quotationmark

Would C# compiler optimize empty void methods away? No. They could still be accessed via reflection, so it's important that the method itself stays. Any call sites are likely to include the call as well - but the JIT may optimize... more 2/11/2015 8:34:49 AM

people

Calling the base class directly in the derived class constructor argument

I have 2 classes: public class LineGeometry { public LineGeometry(Point startPoint, Vector direction) { this.startPoint = startPoint; this.direction = direction;...
Jon Skeet
people
quotationmark

It sounds like you should just call up to the base class constructor: LineSegmentGeometry(Point endPoint, LineGeometry l) : base(l.StartPoint, l.Direction) { this.endPoint = endPoint; } Note that I'm referring to StartPoint and... more 2/11/2015 8:31:49 AM

people

Compile error on asp.net page

I have a simple asp.net webform page which i use to display page related data based on pageID. I am trying to define page level variable so that i can access there variable in...
Jon Skeet
people
quotationmark

You can't just have a statement like: _LangID = int.Parse(Helper.GetAppSetting("LangID_En")); within a class declaration. It would have to be in a method, or constructor, or something like that. However, given that you've just declared... more 2/11/2015 8:19:12 AM

people

typeof(T) for inner list

I am trying to deserialise a xml output from a REST API. After deserialising, I need to check whether the response has valid data. public class Response { public UserWrapper...
Jon Skeet
people
quotationmark

You can't just use generics for this - at least, not easily. You could write something like: public static void IsValidResponse<T>(this Response response, Func<Response, T> firstPropertyFetcher, Func<T, object>... more 2/11/2015 8:02:06 AM

people

assertArrayEquals not working in Junit

I am having issues with my assertArrayEquals in java. It is asking me to create a new method but i am using JUnit so I don't really understand how i can fix this issue. This is...
Jon Skeet
people
quotationmark

If you're using JUnit 4 (as it looks like you are), you presumably aren't extending an existing test class - so you need to call the static assertArrayEquals with the class name, e.g. import... more 2/10/2015 11:02:34 PM

people

LINQ Getting all child records from all parents

I have two models: class Foo { public List<Bar> Bars { get; set; } } class Bar { public int Value { get; set; } } Having an instance of List<Foo>,...
Jon Skeet
people
quotationmark

SelectMany is normally the way to flatten hierarchies, so: var values = myList.SelectMany(foo => foo.Bar) .Select(bar => bar.Value); The SelectMany will give you an IEnumerable<Bar>, and then the Select... more 2/10/2015 2:34:54 PM

people

Converting DXL timestamp to C# DateTime

totally messed up with Lotus Notes DXL timestamp format... Given is a timestamp of an exported DXL from a Lotus Notes document, which looks like...
Jon Skeet
people
quotationmark

The problem is your text format - you've used hh which is the 12-hour clock, but you've got a value of 13. You want HH, which is the 24-hour clock. I'd also recommend quoting the T as you just want the literal T character, and also taking... more 2/10/2015 1:01:38 PM

people

Is wrapping an "Unchecked cast from Object to Map<String,Object>" in a try / catch(ClassCastException) enough to avoid run time problems?

I am writing a recursive converter for a Map<String, Object> (meaning the Object part can also be a Map<String, Object>, and so on... The code snippet: if(value...
Jon Skeet
people
quotationmark

Is the catch (ClassCastException ccex) block above enough to avoid any issues with that unsafe cast? No, precisely because it's not checked. It will check that it's a Map, but that's all it is at the JVM level, due to type erasure. It... more 2/10/2015 12:56:33 PM

people