Browsing 7239 questions and answers with Jon Skeet

Java Check Not Null/Empty else assign default value

I am trying to simplify the following code. The basic steps that the code should carry out are as follows: Assign String a default value Run a method If the method returns a...
Jon Skeet
people
quotationmark

Sounds like you probably want a simple method like this: public String getValueOrDefault(String value, String defaultValue) { return isNotNullOrEmpty(value) ? value : defaultValue; } Then: String result =... more 7/14/2015 4:31:13 PM

people

c# Bits order in byte

Hello I'm trying to understand how to get or set bit and I'm stuck in bit order. Let's say I have a number 70 which is 01000110. I want to change first bit to true so it becomes...
Jon Skeet
people
quotationmark

Why is first bit in byte changed with index of last? Basically, bits are usually referred to such that the least-significant bit is bit 0, then the next is bit 1 etc. So for example: Bit: 76543210 Value: 01000110 So a byte with... more 7/14/2015 4:19:36 PM

people

C# check XmlNode value attribute

I wrote this code to check if a XmlNode has a value, but when I run it crash always on the !=null. It is strange because this solution is well known. private static void...
Jon Skeet
people
quotationmark

You only need to check whether the attribute indexer itself returns null: if (node.Attributes["SplitCombinationOperator"] != null) Currently, that's returning null, and you're dereferencing it for the Value property - hence the... more 7/14/2015 3:36:59 PM

people

create a list Using combination of two types

I need to create a list in which each member is a combination of two types A and B. Is it possible? For example, assuming A and B: class A { int a1; string a2; } class...
Jon Skeet
people
quotationmark

Three options: Create a new class or struct composing both: public class AAndB { private readonly A a; private readonly B b; public AAndB(A a, B b) { this.a = a; this.b = b; } // etc } Use... more 7/14/2015 12:32:11 PM

people

Overriding the message of exception in C#

Is there a way to override the message of an exception? I don't want to make a custom exception but to override the message of an existing exception. For example: Every time when...
Jon Skeet
people
quotationmark

For exceptions you're throwing, you can just pass the message in to the constructor: throw new ArgumentOutOfRangeException("name", "My custom message"); Note that here, name is the name of the parameter that caused the problem. In C# 6,... more 7/14/2015 6:11:36 AM

people

Make instance of Singleton Class object eligible for GC

I have a class JAXBReader which hold unmarshalled xml file using the jaxb generated classes. I have used the singleton design so that I need not unmarshall the file again and...
Jon Skeet
people
quotationmark

One option would be to do all of this within the static initializers of the enum. Keep a static field within the enum itself, write a private static method to get the contents of the file, reading it if necessary, and then at the end of... more 7/14/2015 5:51:11 AM

people

Converting TimeSpan to milliseconds in linq query

I'm trying to add a TimeSpan to a DateTime in a linq query using entity framework. I've got the following code: EDIT TimeSlotsis an ICollection<TimeSlot> TimeSlot has the...
Jon Skeet
people
quotationmark

I suspect the problem is that you're using Seconds instead of TotalSeconds. The Seconds property only gives a value in the range [-59, 59] - so 5 minutes and 30 seconds would return 30, not 330, for example. Try: end =... more 7/13/2015 4:19:05 PM

people

Is there any restriction for writing property names C#

I've written a class definition like public class Item { public double? 30dhi { get; set; } public double? 30dlo { get; set; } ...
Jon Skeet
people
quotationmark

Why it's not allowing me to add properties having alphanumeric keys A property name has to be an identifier. Identifiers in C# can't start with a digit. You can have a digit after the first character, but not as the first... more 7/13/2015 5:53:07 AM

people

Compiler complains "actual and formal argument lists differ in length", but they do not

When I compile the following code: import com.google.common.collect.ImmutableMap; import java.util.Map; import java.util.function.Function; import...
Jon Skeet
people
quotationmark

Currently you're trying to specify 5 type arguments here: Testcase. <String, String, Integer, Map<String, Integer>, ImmutableMap<String, Integer>>toImmutableMap2( ... but the method only has 3 type... more 7/12/2015 4:09:26 PM

people

Use a system class with a different name?

I need a way to track the numbers of rows and columns in a grid. If I use System.Point, I am always going to be forgetting whether "x" is the number of rows or columns. So I...
Jon Skeet
people
quotationmark

No, you can't "rename" members like that. You can refer to System.Point as NRowsColumns if you really want, as using NRowsColumns = System.Point; ... but it would still have the same members as System.Point. It would be simpler to just... more 7/12/2015 7:53:25 AM

people