Browsing 7239 questions and answers with Jon Skeet

Best way to get runtime Type of T?

Given the example code below, I'd like to get the runtime type of T inside a generic method (in this case retrieveData). When I create an XMLFilePersisting I use the interface,...
Jon Skeet
people
quotationmark

I would suggest that you make each XMLFilePersisting use the specific concrete type, but then combine the results into a List<ITransferable>. For example: // Names changed to be more conventional var class1Loader = new... more 2/7/2015 9:01:43 AM

people

Can the Number class be used as an argument to accept numbers of all types as parameters?

I would like the method to accept ints, and doubles. Can the Number class be used as an argument, so that all numbers can be accepted as parameters. (I suspect that it could...
Jon Skeet
people
quotationmark

Yes, autoboxing works to convert int to Number just as well as from int to Integer: public class Test { public static void main(String[] args) throws Exception { foo(10); foo(10.5); foo(10.5f); ... more 2/7/2015 8:37:32 AM

people

Cannot access method in other project of java

I have create one java project which has following class with it's body. package tfimvalidation; public class ValidateToken { public void display() { ...
Jon Skeet
people
quotationmark

You can't just call a method in a class declaration like that. You can declare fields in a class declaration, but method calls (other than those used for field initializers) have to be in methods or constructors. For example: import... more 2/7/2015 8:25:11 AM

people

csv to xml and lambda expressions

I have the following piece of code to convert a csv to an XML. var xml = new XElement("root", lines.Where((line, index) => index > 0).Select(line => new...
Jon Skeet
people
quotationmark

Firstly, it really helps if you indent more clearly: var xml = new XElement("root", lines.Where((line, index) => index > 0) // Skips first line .Select(line => new XElement("TEST", line.Split(',') ... more 2/6/2015 5:27:30 PM

people

Wrapping ulong variables with arithmatic operators

I'm somewhat confused regarding the nature of intentionally overflowing structures like ulong in C#. Essentially I'm working on a grid that I want to be ulong.max X ulong.max...
Jon Skeet
people
quotationmark

However, it seems if I multiply two ulongs together and it results in an overflow, I end up with zero instead of the "modulus(ulong.max)" of whatever I would have otherwise. You wouldn't get the result modulus(ulong.max) - you would... more 2/6/2015 2:51:45 PM

people

How do I refactor a ReadLine loop to Linq

I'd like to make below code cleaner (in the eye of the beholder). var lines = new StringReader(lotsOfIncomingLinesWithNewLineCharacters); var resultingLines = new...
Jon Skeet
people
quotationmark

Basically you need a way of extracting lines from a TextReader. Here's a simple solution which will only iterate once: public static IEnumerable<string> ReadLines(this TextReader reader) { string line; while ((line =... more 2/6/2015 12:21:51 PM

people

Why isn't an overflow exception raised?

The following runs fine without error and diff is 1: int max = int.MaxValue; int min = int.MinValue; //Console.WriteLine("Min is {0} and max is {1}", min, max); int diff = min -...
Jon Skeet
people
quotationmark

By default, overflow checking is turned off in C#. Values simply "wrap round" in the common way. If you compiled the same code with /checked or used a checked { ... } block, it would throw an exception. Depending on what you're doing,... more 2/6/2015 9:28:18 AM

people

why explicit type casting required from double to float but not from int to byte?

Consider following statement: byte by = 5; //works fine literal '5' is of type int and small enough to fit into a variable of type byte. Compiler does the implicit type...
Jon Skeet
people
quotationmark

In the integer version, the compiler knows that all the data in the number 5 can be stored in a byte. No information is lost. That's not always true for floating point values. For example, 0.1f isn't equal to 0.1d. Now for the example,... more 2/6/2015 8:28:59 AM

people

How to use underlying generic types of generics C#

I have an internal generic method byte[] catchAllOperation<T>(T genericItem) and another one byte[] arrayOperation<T>(T[] genericArray). Additionally, I have a highly...
Jon Skeet
people
quotationmark

Your question isn't entirely clear to me, but it sounds like you basically need to look in typeof(T) with reflection. Fortunately, dynamic typing in C# means you can get the compiler team to do the hard work - admittedly at execution... more 2/6/2015 8:08:29 AM

people

C# implement interface on "where TEntity : class"

I'm relatively new to .NET and I've stumbled on this particular issue: while following a tutorial on repository pattern, the definition of the class goes like this: public class...
Jon Skeet
people
quotationmark

Since I already used the : operator, how do I do that? : isn't an operator - you're just using it in the generic type constraint. You can still specify an interface to implement. This should be fine: public class... more 2/5/2015 9:55:09 PM

people