Browsing 7239 questions and answers with Jon Skeet

Interface Cast Type Safety

Type safety prevents unwanted casting during compile-type. How can one achieve the same compile-time type safety for inherited interfaces? public interface IBase { } public...
Jon Skeet
people
quotationmark

You can't, and shouldn't - because it might not be an error. Consider an extra class: class BarBase : IBase, IBar { } Now: IBase item = new BarBase(); IBar bar = item as IBar; That will leave bar as a non-null reference - so why... more 12/16/2015 9:34:29 AM

people

How to write ASCII in the OutputStream

According to the Datamax Documentation: <CR> is used to identify the line termination character. Other strings placed between < > in this manual represent the...
Jon Skeet
people
quotationmark

Here, <CR> means "carriage-return", i.e. "\r". However, if you've got an OutputStream and you want to write text to it, I would suggest wrapping it in an OutputStreamWriter, specifying the appropriate encoding. You can then use... more 12/16/2015 7:25:50 AM

people

Data not inserting in database when using console application

I am new to console application. Now i wrote a sample function for insert values in database. There is no problem in my database connection. But my valuse are not inserting in...
Jon Skeet
people
quotationmark

You're not executing the command at all - you're creating a SqlCommand, and then doing nothing with it. You need to call ExecuteNonQuery(). However, you should also stop building the SQL like that. You should start using parameterized SQL... more 12/16/2015 7:20:09 AM

people

Create nodes from List<XElement> using linq

I want to create the following the xml <BookStore> <Book> <Name></Name> <Author></Author> <Price></Price> ...
Jon Skeet
people
quotationmark

It sounds like you basically need to take the list of elements and group them into groups of 3 elements, putting each group in a Book element: // The name/author/price elements you're already getting var elements = ...; var groups =... more 12/15/2015 10:47:13 PM

people

C# extend method for Enum object

I am trying to create an extension method but can't get it to work. So this works, create extension method on a Type of enum example: public enum Pets { .... } Above Pets...
Jon Skeet
people
quotationmark

This has nothing to do with Enum as such - but extension methods "look" like they're instance methods. You can't "pretend" to add static methods, as it looks like you're currently trying to do. You would be able to... more 12/15/2015 7:17:28 PM

people

Getting local hour with NodaTime

New to NodaTime and I chose to use it instead of the BCL libraries because it removes a lot of ambiguity when dealing with dates. However, I can't seem to get it to do what I...
Jon Skeet
people
quotationmark

It sounds like your initial date/time is actually in Central time - so once you've performed the initial conversion, you can just say "Hey, I want the same instant in time, but in a different time zone" - which isn't the same as "I want a... more 12/15/2015 6:01:30 PM

people

What's the best practices to handle frequently changing timezones

I'm writing a toy logistics service. As a result, products will travel through lots of different timezones, and I'll need to query both the strict ordering of events, as well as...
Jon Skeet
people
quotationmark

Given that you're recording events as they occur (rather than planning for future events), you probably don't need to worry about changes to time zone rules which occur in the future. As such, it would be reasonable to store a timestamp... more 12/15/2015 5:38:30 PM

people

C# filtering a collection of objects

I'm writing a method which returns a collection of ProductPeriod objects based on the following filters: DateTime? from DateTime? to bool? includeActive bool?...
Jon Skeet
people
quotationmark

Yes, there's definitely a better way. You should use the way that queries can be built up in LINQ: public IEnumerable<ProductPeriod> GetFilteredProductPeriods (DateTime? from, DateTime? to, bool? includeActive, bool?... more 12/15/2015 12:14:07 PM

people

Decoding Base64urlUInt encoded value

What I am generally trying to do, is to validate an id_token value obtained from an OpenID Connect provider (e.g. Google). The token is signed with the RSA algorithm and the...
Jon Skeet
people
quotationmark

RFC 7515 defines base64url encoding like this: Base64 encoding using the URL- and filename-safe character set defined in Section 5 of RFC 4648, with all trailing '=' characters omitted (as permitted by Section 3.2) and without the ... more 12/15/2015 9:12:52 AM

people

using of casting in inheritanceHierarchy?

Please see the code demo as the following and I have misunderstanding of inheritanceHierarchy problem about casting. Just the example shows below, the correct result is Baz 1 Foo...
Jon Skeet
people
quotationmark

I think that casting means make one object type turn into another one, so e is Foo type now and just check method one in Foo type. No. Casting doesn't change the type of the object at all. It only gives you an expression of the target... more 12/15/2015 7:25:15 AM

people