Browsing 7239 questions and answers with Jon Skeet

How to implement a dynamic generic type return method without casting at runtime in C#?

Say I want to convert the below implementation in Java to C#, so that I don't have to cast the returned value at runtime, where the casting should already be handled in the get...
Jon Skeet
people
quotationmark

I'm not sure I really like it as an idea - but you could do this by making Attribute generic: public static class Attributes { public static Attribute<int> Age = new Attribute<int>("age"); public static... more 7/25/2015 7:35:15 PM

people

Why do I have to cast an inherited class when it's proven that it is the base class?

I have a class called BonusCell. It inherits the GameEntity class. public class BonusCell : GameEntity { public void GiveStats() { } } I have a class called...
Jon Skeet
people
quotationmark

Why do I have to cast BonusCell even if I am using an if is statement to verify that it is a BonusCell? Because the is operator doesn't change the compile-time type of the expression board.grid[x, y].gameEntity. (In this case, I guess... more 7/25/2015 5:29:34 PM

people

How to use parameters and arguments in java?

I don't understand the connect or the difference between parameters and arguments. Can you use either as a method? How do you return an argument? Any help is greatly appreciated.
Jon Skeet
people
quotationmark

Warning: a lot of people don't differentiate between "parameter" and "argument". They should, but they don't - so you may well see a lot of pages with incorrect uses of the terms. When you declare a method or constructor, the parameters... more 7/25/2015 9:34:03 AM

people

Compiler gives implicit conversion error || My generic method's constraint is an abstract generic class

I'm writing a program and found some common behavior, so I thought this would be an appropriate use case for an abstract base class. This is a simplified version of my Abstract...
Jon Skeet
people
quotationmark

It seems to me that you need two generic type parameters: public void Import<TBehavior, TDomain>() where TBehavior : BehaviorClass<TDomain> where TDomain : IDomainObj { var instance = (TBehavior)... more 7/24/2015 8:16:23 PM

people

Unity Array of Struct : When setting a variable of one of the array subscripts, it sets it for all of them

This is my struct that I have created. public struct Bar { private static float deltaTime = 1.0f; private static bool AutoRun = false; private static bool...
Jon Skeet
people
quotationmark

Your fields are all static: private static float deltaTime = 1.0f; private static bool AutoRun = false; private static bool AutoRunBought = false; private static bool Start = false; So if you write: Bar x = new Bar(); Bar y = new... more 7/24/2015 5:12:47 PM

people

Java Calendar TimeZone mess

Here is my simple code: String defaultSimpleDateFormatPattern = "MMM dd, yyyy HH:mm:ss"; TimeZone tzNY = TimeZone.getTimeZone("America/New_York"); TimeZone tzLos =...
Jon Skeet
people
quotationmark

There are two problems here: You're using an invalid time zone ID (you want America/New_York) You're parsing using a formatter that hasn't got a time zone set (so it'll use the default time zone) and then setting the time zone in the... more 7/24/2015 4:11:07 PM

people

TimeZone.setDefault for a thread in Java 8

We currently use TimeZone.setDefault to set the default timezone for a thread. I know that with Java 8 calling this method sets the default timezone for the JVM. Does anyone have...
Jon Skeet
people
quotationmark

Don't use the system defaulting at all. Use ThreadLocal<T>: either a ThreadLocal<TimeZone>, or better, a ThreadLocal<ZoneId> with the java.time classes. Then you can fetch from there everywhere that you need the... more 7/24/2015 3:04:23 PM

people

LINQ expression to generate a list of days between two dates

I have a start and an end date and I want to get a list of all the days between those 2 dates (inclusive). I can do this with a loop that adds 1 day to the start date then adds...
Jon Skeet
people
quotationmark

If you know the start and end, it's simple just to write a method using an iterator block to do this: public static IEnumerable<DateTime> DateRange(DateTime startInclusive, DateTime endInclusive) { for (var current =... more 7/24/2015 2:14:19 PM

people

why 32 bit signed integer maximum value change to minimum value after increment by 1 in c#?

If I have integer variable with maximum value assigned it which is (2,147,483,647) for 32 bit integer, and if I am incrementing it by 1 then it turn to (-2147483648) negative...
Jon Skeet
people
quotationmark

This is just integer overflow, where the value is effectively leaking into the sign bit. It's simpler to reason about it with sbyte, for example. Think about the bitwise representations of 127 and -127 as signed bytes: 127:... more 7/24/2015 12:28:52 PM

people

Replacing if else within 'for' loops with Java 8 Streams

I have following simple code that I am trying to convert to functional style for(String str: list){ if(someCondition(str)){ list2.add(doSomeThing(str)); } ...
Jon Skeet
people
quotationmark

It sounds like you can just use map with a condition: List<String> list2 = list .stream() .map(str -> someCondition(str) ? doSomething(str) : doSomethingElse(str)) .collect(Collectors.toList()); Short but complete... more 7/24/2015 11:47:01 AM

people