Browsing 7239 questions and answers with Jon Skeet

Why do I need to use M when declaring a decimal

When I try to declare a variable like this: decimal order = 5.0; I get the error message "Literal of type double cannot be implicitly converted to type 'decimal'; use an 'M'...
Jon Skeet
people
quotationmark

Any numeric literal with a decimal point but no suffix is of type double. From the C# 5 specification, section 2.4.4.3: If no real-type-suffix is specified, the type of the real literal is double. Otherwise, the real type suffix... more 7/22/2015 7:09:14 PM

people

File.WriteAllLines does not appear to be closing

I am trying to add a feature to a website that will send an outlook invite attachment to email. Everything runs the first time perfectly, but after running it again, I get the...
Jon Skeet
people
quotationmark

I suspect the problem isn't with File.WriteAllLines, but with Attachment - that implements IDisposable, but you're not disposing of it... so if that's got an open file handle on it, that would explain what you're seeing. You can just... more 7/22/2015 7:07:00 PM

people

Using ExpandoObject when property names are dynamic, is this possible?

I need to create an object that has properties that are named dynamically like: <users> <user1name>john</user1name> <user2name>max</user2name> ...
Jon Skeet
people
quotationmark

Yes, absolutely. Just use it as an IDictionary<string, object> to populate: IDictionary<string, object> expando = new ExpandoObject(); expando["foo"] = "bar"; dynamic d = expando; Console.WriteLine(d.foo); // bar In your... more 7/22/2015 2:52:59 PM

people

cannot be cast to ThreadPoolTaskExecutor

I have to write a class which is accept the socket connection and pass it to Handler class using Spring My Listener Class is public class Listener { static Logger log =...
Jon Skeet
people
quotationmark

You're requesting the TCPHandler bean here: context.getBean("TCPHandler") That isn't a ThreadPoolTaskExecutor, so I don't know why you expect it to be. If you want to retrieve the executor, you should be fetching that bean instead,... more 7/22/2015 10:52:38 AM

people

Memory efficient way to initialize a String to be reused inside a loop

I'm using a couple of Strings in my code that are going to be reused within a loop and I'm wondering what would be the best way to initialize the String variables to improve...
Jon Skeet
people
quotationmark

I'm using a couple of Strings in my code that are going to be reused within a loop No, you're not. You're using a few string variables that are going to be reused within the loop. The actual strings aren't. Here's the body of the loop... more 7/21/2015 7:25:02 PM

people

Compile Cplex in Java in linux

i'm trying to compile my file.java with an optimization problem with CPLEX notation in a cluster wich use linux (an i'm a windows user) and a used (through and application to...
Jon Skeet
people
quotationmark

Use the -cp command line argument to add the jar file to your compile-time classpath. You'll need to specify the classpath when you run the code too. $ javac -cp /home/apps/cplex/12.6.1/cplex/lib/cplex.jar file.java $ java -cp... more 7/21/2015 3:18:23 PM

people

Bitwise or operator used on a sign extended operand in Visual Studio 2015

I just tried installing Visual Studio 2015, and when trying to compile an old project, I got the warning CS0675 Bitwise-or operator used on a sign-extended operand; consider ...
Jon Skeet
people
quotationmark

Sign extension is happening, but possibly not for the obvious reason, and not in a worrying way, IMO. This code: a |= (short) b; is equivalent to: // No warning here... (surprisingly, given that `a` is being sign-extended...) a =... more 7/21/2015 2:47:27 PM

people

For a property found by reflection how can I test for Listness and enumerate over it?

How can I test whether a class member is a list when I don't know it's exact type? And how do I then iterate over it? myc is an instance of MyClass using...
Jon Skeet
people
quotationmark

If all you want to do is enumerate the values, you could test for the non-generic IEnumerable instead: object value = pi.GetValue(myc, null); IEnumerable enumerable = value as IEnumerable; if (enumerable != null) { foreach (object obj... more 7/21/2015 2:32:39 PM

people

C# Move file from bool

I have this code: bool containsNonAllowedCLEOFiles = directory.EnumerateFiles().Any(file => !allowedCLEOFiles.Contains(file.Name)); if (containsNonAllowedCLEOFiles ==...
Jon Skeet
people
quotationmark

Well it sounds like you should first find those files, then check whether or not there are any: var invalidFiles = directory.EnumerateFiles() .Where(file => !allowedCLEOFiles.Contains(file.Name)); ... more 7/21/2015 2:10:21 PM

people

Can I share a variable between machines using C#?

I have a C# application running in machine_1 (local PC). This application writes values to a Global Variable. I just want to read the value of the Global Variable using another C#...
Jon Skeet
people
quotationmark

No, not a variable as such - not with a regular CLR. (It would be possible to write a VM which did share variables across the network, but it would be quite unusual.) Instead, you'll need to establish some sort of protocol between the two... more 7/21/2015 6:13:17 AM

people