Browsing 7239 questions and answers with Jon Skeet

C# Generic Shorthand and Upcasting

I've been playing with interfaces and generics. What I want is an Foo<T> to be a shorthand for Foo<T, int> Is this sensible? Is the following an accepted way of doing...
Jon Skeet
people
quotationmark

What my actual question is... is it possible to up (down?) cast from a Foo<T, int> to a Foo<T> where T is the same type. Does it even make sense to say this, or is this something to do with co/contravariance (which still... more 5/24/2016 8:45:57 PM

people

Will the compiler create extra strings if a string reference is set to strings in quotes?

Normally I wouldn't ask this here, but I don't seem to have found any articles on what happens in this circumstance. Take the following example: string s = "Potato"; string t =...
Jon Skeet
people
quotationmark

In the second line, will the compiler optimize this to making a one string with Carrot added to the beginning and bean to the end, or will it create strings for Carrot and Bean, and then concatenate each to create the string? The... more 5/24/2016 4:24:32 PM

people

Linq group by multiple column not working, lambda syntax

I am trying to group 7 objects (of Type ABPM) by date and hour and not getting correct results. Private Class ABPM Public DT As Date = Date.Now Public Hour As Integer =...
Jon Skeet
people
quotationmark

You need to make your anonymous type use Key properties, so that they participate in equality computations: Dim hourlyDayGroup2 = abpms.GroupBy(Function(a As ABPM) New With {Key a.DT, Key a.Hour}) From MSDN: Key properties differ... more 5/24/2016 4:14:55 PM

people

Singleton implementation laziness with static constructor

Jon Skeet suggests in his singleton implementation that if you require the maximum laziness for your singleton you should add a static constructor which will make the compiler...
Jon Skeet
people
quotationmark

When I call Singleton.Stub() the private constructor is not being hit, when I uncomment the static ctor private constuctor is always called. It's not clear what the value of which is here, but fundamentally you've got four... more 5/24/2016 2:15:42 PM

people

Cannot implicitly convert type 'string' to 'integer'

Here is MyCode: public ActionResult Index() { var EmployeerId = SessionPersister.Email_ID; //Convert.ToInt32(EmployeerId); int.Parse(EmployeerId); ...
Jon Skeet
people
quotationmark

Just calling Convert.ToInt32(EmployeerId) or int.Parse(EmployeerId) doesn't change the type of EmployeerId - both methods will return a value that you'd need to store in a new variable. You're also not doing anything with your query... more 5/24/2016 7:55:10 AM

people

StreamWriter ReadLine method hangs program

I am trying to get some basic multi-threading working in C#. There's no clear tutorial that I've found so bear with my sub-par code: class Program { private static...
Jon Skeet
people
quotationmark

Your client code is broken here - you're never flushing it: var sw = new StreamWriter(ns); Thread.Sleep(1000); sw.WriteLine(handshake); There's no need for the Thread.Sleep call, but you should flush the StreamWriter if you want the... more 5/23/2016 5:28:36 PM

people

C# DateTime to SQL DateTime losing precision

Have a small problem where if I save a DateTime field as an SQL command parameter it loses precision, like often less than a milisecond. e.g. The parameter's Value is: TimeOfDay ...
Jon Skeet
people
quotationmark

You're using DateTime, which is documented with: Accuracy: Rounded to increments of .000, .003, or .007 seconds It sounds like you want DateTime2: Precision, scale: 0 to 7 digits, with an accuracy of 100ns. The default precision... more 5/23/2016 4:21:44 PM

people

Inherited global variable cannot be accessed

I am writing a small program that is supposed to create differently shaped rooms from a base of a square. The shapes are supposed to inherit from the square, which contains List...
Jon Skeet
people
quotationmark

According to all the tutorials I found, this should be working, even if the Parent's members are private. No, private members are only accessible within the program text of the declaring type. So if you declare LShape as a nested type... more 5/23/2016 3:21:43 PM

people

In Java, how to convert correctly byte[] to String to byte[] again?

I want to convert the result of a TEA encryption (a byte[]) to a String and then convert it again to a byte[] and retrieve the same byte[]. //Encryption in the sending...
Jon Skeet
people
quotationmark

Don't try to convert from the byte[] to String as if it were regular encoded text data - it isn't. It's an arbitrary byte array. The simplest approaches are to convert it to base64 or hex - that will result in ASCII text which can be... more 5/23/2016 10:42:55 AM

people

Why cant we use IteratorStateMachineAttribute in C#?

I did a Go To Definition (F12) on a class I was trying to derive from and I noticed that one of the methods was marked with AsyncStateMachineAttribute. Which in turn inherits...
Jon Skeet
people
quotationmark

I'm 99% sure it's historical. Basically, C# introduced iterator blocks in C# 2 - a long time before this attribute was introduced. The equivalent async attribute was introduced at the same time as async methods in C#, so that was fine...... more 5/23/2016 9:31:37 AM

people