Browsing 7239 questions and answers with Jon Skeet

Does the LINQ Expression API offer no way to create a variable?

I want to validate my assumption that the LINQ Expression API does not have any means for us to create an expression that represents the creation of a local variable. In other...
Jon Skeet
people
quotationmark

Well, you can use Expression.Block to declare a block which contains local variables... For example: using System; using System.Linq.Expressions; public class Test { static void Main() { var x =... more 5/8/2015 11:03:38 AM

people

Does XElement have built in support for nil=true

I have the following xml parsed into an XElement named entry. <Person> <Name>Ann</Name> <Age i:nil="true"...
Jon Skeet
people
quotationmark

No, I don't believe there's anything for this, but it would be dead easy to write an extension method: private static readonly XNamespace ns = "http://www.w3.org/2001/XMLSchema-instance"; public static string NilAwareValue(this XElement... more 5/8/2015 11:01:26 AM

people

Decode base64 filestream with FromBase64Transform

The example from MSDN seems to read the whole file into memory. I don't want that. The file should be processed block by block. Therefore I tried to rewrite the example: using...
Jon Skeet
people
quotationmark

I believe this line is the bug: int bytesWritten = myTransform.TransformBlock (inputBuffer, 0, 4, transformBuffer, 0); You're transforming 4 bytes, regardless of how many bytes you've read. I suspect you want: int bytesWritten =... more 5/8/2015 10:17:09 AM

people

Thread safety in String class

Is it thread safe to build strings from local variables using the String class like in the methods below? Suppose that the methods below are called from several threads. public...
Jon Skeet
people
quotationmark

That's absolutely fine. There is no shared state in either piece of code other than the string literals. As strings are immutable, it's fine for strings to be shared freely between threads, and both string.Format and string.Concat... more 5/8/2015 9:47:12 AM

people

Multi threading instance field implementation

public class Chicks { synchronized void yacks(long id) { for(int x = 1; x<3; x++) { System.out.println(id + " "); ...
Jon Skeet
people
quotationmark

Your go method assigns a non-null value to "this" instance of ChickYacks - but then creates two new instances of ChickYacks, each of which will have a null value for c. You could: Initialize c in a constructor instead, so that each... more 5/8/2015 7:04:54 AM

people

Value and reference type in generic type implementation

I searched about generic type in C# and I made this conclusion: All reference types are based on Class All value types are based on struct The main differences between struct...
Jon Skeet
people
quotationmark

What you're describing are generic constraints. Where T:New() == the generic parameter must be a class + have a default empty constructor No, that just says "the type argument must have a parameterless constructor". That actually... more 5/7/2015 7:53:54 PM

people

Proper way in C# to combine an arbitrary number of strings into a single string

I breezed through the documentation for the string class and didn't see any good tools for combining an arbitrary number of strings into a single string. The best procedure I...
Jon Skeet
people
quotationmark

If you want to insert a separator between values, string.Join is your friend. If you just want to concatenate the strings, then you can use string.Concat: string assetUrl = string.Concat(assetUrlPieces); That's marginally simpler (and... more 5/7/2015 4:39:26 PM

people

Android : Convert millisecond to time

I want return millisecond to time But my code not work ! long ms = 86400000; long s = ms % 60; long m = (ms / 60) % 60; long h = (ms / (60 * 60)) % 24; String timeFind =...
Jon Skeet
people
quotationmark

You could use SimpleDateFormat, but be aware that you should set both the time zone and the locale appropriately: DateFormat formatter = new SimpleDateFormat("HH:mm:ss",... more 5/7/2015 4:35:40 PM

people

Using a handler to (WriteFile) returns binary? Garbage?

I have a physical path to a file. c:\fullpath\filename.jpg But when using: context.Response.WriteFile(fullPath) I end up with stuff...
Jon Skeet
people
quotationmark

You're writing the content of a binary file to the response. We don't know what else you've written to the response, or what you've set the content type to. If your response content type is text/plain or something similar, then that's the... more 5/7/2015 4:09:22 PM

people

What are the reasons to optimize imports?

Are there any technical reasons to "optimize imports" via your editor? Eclipse, Intellij IDEA, and NetBeans all have ways to optimize imports. I'm wondering if there are reasons...
Jon Skeet
people
quotationmark

If I understand right, in the above example the classloader will load the Map and List classes before MyClassThatUsesMap. You don't understand right. Imports have nothing to do with execution-time handling. They only affect how the... more 5/7/2015 2:01:37 PM

people