Browsing 7239 questions and answers with Jon Skeet

Converting a Predicate<T> to a Func<T, bool>

I have a class with a member Predicate which I would like to use in a Linq expression: using System.Linq; class MyClass { public bool DoAllHaveSomeProperty() { ...
Jon Skeet
people
quotationmark

The two types represent the same logical signature, but that doesn't mean they're just interchangable. A straight assignment won't work, for example - but you can create a new Func<T, bool> from the Predicate<T, bool>. Sample... more 8/27/2014 4:07:31 PM

people

Rounding bug in c#?

I'm having problems with a Rounding issue in c#. I would like to round the result of a calculation up to 4 decimals (awayfromzero). If I use Math.Round(variable,...) it rounds...
Jon Skeet
people
quotationmark

If you use my DoubleConverter, you can see the exact value of result: Console.WriteLine(DoubleConverter.ToExactString(result)); That prints: 591.2457499999999299689079634845256805419921875 ... which rounds to 591.2457 when rounded to... more 8/27/2014 3:56:04 PM

people

How to create public calculation function that returns calculated value?

I would like to create public function, that will be used to calculate some value. I would like the value to be the output of the function, for example: public void...
Jon Skeet
people
quotationmark

Currently, you're assigning a value to a variable, and declaring that your method doesn't return anything. Instead, you should just make it return the result: public int calculate(...) { int x = y+z/2 +i; if(x >= 10) { ... more 8/27/2014 2:28:32 PM

people

Exception in SqlConnection constructor causing exception in finalize

I have the following code: SqlConnection ret = new SqlConnection(connectionString); This throws a ConfigurationException which I catch, however, since the exception occurs in...
Jon Skeet
people
quotationmark

I think it's reasonable for an issue in the machine.config file to mean that an app simply can't run. If your system-wide configuration is broken, you (the owner of that broken configuration) should fix it. Not all failures can be... more 8/27/2014 8:12:35 AM

people

org.w3c.dom.Node.setNodeValue() not encoding certain special characters

Using Java, I set the value of a <value> attribute in an xml file to be String val = "~!@#$%^&*()_+-={}|[]\\:\";'<>?,./;" When I tried to use the api with input...
Jon Skeet
people
quotationmark

It's simply because it doesn't need to escape >, ' or ". The quotes would only need to be encoded in an attribute which was opened with the same kind of quote (so foo="single'quote" or bar='double"quote' is fine) and the closing angle... more 8/27/2014 5:53:51 AM

people

Confused accessing delegate from WPF ui thread

My apologies but I can't see what I am doing wrong / not understanding? I have declared a delegate such as: namespace ABC { public delegate void GenerateView(); public...
Jon Skeet
people
quotationmark

The reason your current code doesn't work is that handler is just a local variable within the constructor. While you could add a public property, if the point is just for external code to be able to get a delegate which calls... more 8/27/2014 5:46:16 AM

people

Instantiate a Generic class via Reflection

I want to instantiate a class via reflection as follows: public class TestConsume<T> : BaseExecutor<T> { public overide SomeMethod(T input) { } } Where the...
Jon Skeet
people
quotationmark

To get Assembly.GetType to work, you need to use the CLR name, which includes the generic arity - the number of type parameters it has: Type type = Assembly.GetExecutingAssembly().GetType("Test.TestConsume`1"); That's assuming you need... more 8/26/2014 8:47:15 PM

people

DateTime.Date get me Date and 12:00 AM

I have already seen this. I am going to get only Date from a DateTime variable and in this way I used this code: DateTime Start = GetaDateTime(); String Day =...
Jon Skeet
people
quotationmark

Actually I should not get 12:00AM any more???? Why not? A DateTime has no notion of whether it's meant to be a date or a date and time, or any sort of string formatting. It's just a point in time (and not even quite that, given the... more 8/26/2014 7:58:05 PM

people

Reading xml string with c#

I have the following soap response string: <EncryptionInfoResponse> <Status>0</Status> <Message></Message>...
Jon Skeet
people
quotationmark

The problem is that you're using XElement.Parse, so you're parsing the string as an element... so xdoc is already the EncryptionInfoResponse element, which doesn't have any more EncryptionInfoResponse descendants. Options: Use... more 8/26/2014 7:49:57 PM

people

Instance Method Reference and Lambda Parameters

I am having trouble understanding the syntax for a method reference, where there are two parameters a and b, and the reference is to a method of a on b. For example I understand...
Jon Skeet
people
quotationmark

I think you're looking for JLS section 15.13.3, which includes: If the form is ReferenceType :: [TypeArguments] Identifier, the body of the invocation method similarly has the effect of a method invocation expression for a compile-time... more 8/26/2014 6:20:57 PM

people