Browsing 7239 questions and answers with Jon Skeet

Switch Statement with Enum Or (||) and (&&)

I'm making a project and I have a question. I have 3 enum states like this: enum GameLevel { Level1, Level2, None, } There's a part in my code where I want to...
Jon Skeet
people
quotationmark

Various options: Use switch with a default: switch (level) { case GameLevel.None: // Whatever break; default: // Do something else // break } Use switch with explicit cases: // Note: if you add... more 5/6/2015 2:03:23 PM

people

How to get replaced nodes in XDocument

I'm replacing nodes in XDocument, but I can't find a simple way to access them after they were replaced. In the code below I can replace "nodeC" with "newnode", but if I try to do...
Jon Skeet
people
quotationmark

The problem is that when you call Replace, newNode already has a parent - so it's cloned. If you remove it from its parent before calling ReplaceWith, then the element is added directly instead of a copy being... more 5/6/2015 1:51:48 PM

people

Get actual DateTime independent of SystemTime

Objective: I'm developing an application that can work in online as well as offline. The app has a feature to create and save the NOTES inside the application. Also, the app need...
Jon Skeet
people
quotationmark

Two options you could consider: Use Stopwatch, which is designed for measuring elapsed times. Start that when you retrieve the server time, and then you can add the elapsed time to the server time whenever you need to. However, I don't... more 5/6/2015 6:18:43 AM

people

Optimize Linq in C#

I have columns list in which I need to assign Isselected as true for all except for two columns. (Bug and feature). I have used this following code to achieve it and working fine,...
Jon Skeet
people
quotationmark

I have used this following code to achieve it and working fine, but is there any quick or easy way to achieve the same? Well there's a cleaner way to achieve it in my view - just don't use lambdas etc at all: foreach (var item in... more 5/6/2015 6:10:04 AM

people

difference between calling method as static and as class method through reflection

I have a line of code like this: ConfiguationManagerUtils.class.getResource(resourceName); I don't understand why reflection is used here. What is the difference between...
Jon Skeet
people
quotationmark

It's not using reflection at all. The getResource(String) method called in your first snippet simply isn't declared on ConfigurationManagerUtils - it's declared on the Class class, as an instance method. If the second code snippet works as... more 5/6/2015 5:50:37 AM

people

Working around no lambdas in VS 2005

I have the following excerpt from some wonderful legacy code: Private Sub SomeMethod() Dim deductibles As List(Of Integer) = GetDeductibles() ...
Jon Skeet
people
quotationmark

The answer really depends on the language. In C# 2, we didn't have lambda expressions but we did have anonymous methods... so you can write: List<int> deductibles = GetDeductibles(); deductibles.RemoveAll(delegate(int i) {... more 5/5/2015 5:36:21 PM

people

Generics supports only reference conversions not boxing conversions

While reading c# in a nutshell about boxing and unboxing on page 91, author writes this: Boxing conversions are crucial in providing a,unified type system. The system is...
Jon Skeet
people
quotationmark

Well, there's a reference conversion between string and object, in that every string reference can be treated as an object reference. This can be done transparently, with no modification to the value at all. That's why array variance can... more 5/5/2015 5:19:59 PM

people

Can this async/await code be rewritten using Task.WhenAll(...) or something else that makes more sense then awaiting each time?

I have the following piece of code (changed the names of my classes/objects for brevity). It essentially is hitting an external API that allows only a single operation, but my...
Jon Skeet
people
quotationmark

Yes, you should be able to use something like: public async void SendSeveralRequestsAsync(MyClass myClass) { var client = SomeExternalServiceClient(); var tasks = myClass .Select(item => new ExternalServiceRequest { Value... more 5/5/2015 5:06:03 PM

people

how to implement an interface method change in more that 50 classes

I have an interface IMyInterface, which currently have only one member method MyMethod1 having return type void. I have this interface in more than 50 classes, now suppose I...
Jon Skeet
people
quotationmark

I'd just change the method signature in the interface, and follow all the error messages you get. After all, in each method you're going to need to decide what to return, so you should really look at each of those classes individually.... more 5/5/2015 4:14:47 PM

people

Java inheritance and hidden public fields

To simulate some auto-generated classes which looks like this, I made a small jUnit test class to simulate inheritance and hidden fields. public class ClassDerivationTest { ...
Jon Skeet
people
quotationmark

Well you can just cast child to make the compiler resolve the field value with respect to ParentClass instead of ChildClass: ((ParentClass) child).value = "test"; But frankly I would a) avoid non-private fields; b) avoid knowingly... more 5/5/2015 3:29:34 PM

people