Browsing 7239 questions and answers with Jon Skeet

Generic that implements an interface or is a double?

Say I have this generic class: abstract class Foo<T> where T : IBar This is fine up to here, however I would like to also be able to pass double as well. Now I have been...
Jon Skeet
people
quotationmark

I would consider wrapping double in a struct rather than a class. There's no way of saying "T must either be double or implement IBar" but wrapping a primitive value in a struct is entirely reasonable (assuming it actually makes sense for... more 2/27/2014 6:07:11 PM

people

FromBase64String/UTF Encoding

My issue is based around a string of data that I'm getting back from an API call. I'm passing the raw data into FromBase64String and then encoding the byte array back to a...
Jon Skeet
people
quotationmark

I'm expecting a valid pdfsharp return that I'm saving to a file. If it's meant to be a PDF file, I wouldn't try to convert that to a string at all. It's simply not text - it's binary data. It should be as simple as: byte[]... more 2/27/2014 6:02:05 PM

people

How to make sure we have only one instance, and it is disposed in a correct way

In the software I am writing I will read some data from an external device (connected via USB). The drivers I have been given (dll file) are not thread safe and only one instance...
Jon Skeet
people
quotationmark

The operating system is responsible for cleaning up unmanaged resources when the process terminates. So if you're happy for the resources to be allocated from the point at which you first use the resource until the program termination, I... more 2/27/2014 2:23:28 PM

people

Correct way to expose a cancellable Task API

If I want to expose an API which internally Schedules a sequence of Tasks that should be cancellable by the user. e.g. public ??? DoWork() { Task t = new .... ,...
Jon Skeet
people
quotationmark

Should I just accept a CancellationToken as an arg and let the user create the token source if needed? This. But you should quite possibly return a Task as well, so that the user can observe when it's completed etc. This is amenable... more 2/27/2014 1:37:35 PM

people

typecasting long to float implicitly not narrowing

In java the hierarchy for implicit conversion is byte -> short -> int -> long -> float -> double Long can hold 8 bytes of data. Then why it is implicitly...
Jon Skeet
people
quotationmark

It's not about how many bits of data are used. It's about the scale that can be represented. From JLS section 5.1.2 (widening primitive conversions): A widening primitive conversion does not lose information about the overall... more 2/27/2014 11:46:21 AM

people

for loop ends prematurely when objects are removed

Hi I have a problem with a for loop. It looks like this for (int i = 0; i < ObjectManager.Instance.Objects.Count; i++) { if (ObjectManager.Instance.Objects[i] is...
Jon Skeet
people
quotationmark

Three options: If ObjectManager.Instance.Objects is a List<T>, use List<T>.RemoveAll with a predicate, making your code much simpler: // This replaces your whole loop... ObjectManager.Instance.Objects.RemoveAll(x => x is... more 2/27/2014 11:35:30 AM

people

If condition check in linq

Below are the linq query. Here I want to add one condition . Condition : If : Firstname is not empty then select list where (d=d.firstname=="Firstname") ...
Jon Skeet
people
quotationmark

Two options: First, optionally use Where: var events = pointsCore.Categories.SelectMany(c => c.Events); if (!string.IsNullOrEmpty(firstName)) { events = events.Where(e => e.Firstname == firstName); } var result =... more 2/27/2014 11:33:09 AM

people

Changing DateFormat from EU to US entityFramework

Hello I have little problem with DateTime formats in SQL server. I have an app which connects with SQL Server 2012. I need to use EU Dateformat in App. SQL server holds DateTime...
Jon Skeet
people
quotationmark

SQL server holds DateTime in EU Format No, it doesn't. (At least, not if it's in an appropriate column type in the database.) It holds it in a more compact binary format - at least so I assume. That's an implementation detail though.... more 2/27/2014 10:38:28 AM

people

C# The name 'client' does not exist in the current context // Identifier expected // Expected class, delegate, enum, interface, or struct

Im extremely noob in C#. Could you please help me? I get the following errors: The name 'client' does not exist in the current context Identifier expected Expected class,...
Jon Skeet
people
quotationmark

You've got this: using (WebClient client = new WebClient ()); in the list of using directives when you really meant it to be a using statement in the method: static void Main(string[] args) { using (WebClient client = new... more 2/27/2014 10:29:45 AM

people

Convert a specific type of sting in Date Time

I have to convert the below string in DateTime. I have used following code for that but it was not working. DateTime created = DateTime.ParseExact("Sun Feb 23 2014 00:00:00...
Jon Skeet
people
quotationmark

I would parse this as a DateTimeOffset instead of a DateTime - after all, that's the data you've been given. Assuming it's always specified using GMT+... you can use a format string of "ddd MMM d yyyy HH:mm:ss 'GMT'zzz". In particular,... more 2/27/2014 10:25:53 AM

people