Browsing 7239 questions and answers with Jon Skeet

C# issue with custom raising events

I have my class where I define my event: public class EventRaiserUtility { public event EventHandler updateList; public void updateListEvent() { if...
Jon Skeet
people
quotationmark

You're creating a new EventRaiserUtility just before you call updateListEvent (which should have a capital U to follow .NET conventions, by the way; ditto updateList = UpdateList) - but you're creating a separate EventRaiserUtility in... more 10/10/2014 9:19:47 AM

people

Return unique values without removing duplicates C#

I know there are many answers about returning unique values in an array after removing duplicates, but isn't every element in an array unique after you remove the duplicates? I...
Jon Skeet
people
quotationmark

The simplest approach is to use LINQ, grouping by the value, counting the number of elements in each group, and then returning the keys for groups with a single value: var singleOccurrences = array.GroupBy(x => x) ... more 10/9/2014 8:51:47 PM

people

HttpWebRequest collection Initializer C#

When using HttpWebRequest via HttpWebRequest.Create(url) is there an easier way than the following to initialize a HttpWebRequest by using a object initializer: class...
Jon Skeet
people
quotationmark

It sounds like you're looking for a way to initialize the request in a single statement - otherwise just using two statements is simpler. There's a reasonably simple alternative to this, using a lambda expression - although it's pretty... more 10/9/2014 8:42:04 PM

people

DateTime and Stopwatch value comparison

I'm trying to trigger the Stopwatch.reset(); if the elapsed time is equal to the time value stored in a DateTime object workDt by using the .equals() to compare the elapsed time...
Jon Skeet
people
quotationmark

The chances of the elapsed time being exactly the same as the required time - down to the tick - are tiny. Instead, you should see whether at least the right amount of time has passed: if (myStopwatch.Elapsed >=... more 10/9/2014 3:34:17 PM

people

Generic functions and value types

Do generic functions work well with nullable value types, or do they box behind the scenes? For example I have a helper function like this: public static TResult...
Jon Skeet
people
quotationmark

No, that won't box value types - but it will cause a new native implementation of the method to be created (at JIT time) each time you use a different value type for TResult, whereas all reference types will share the same native code.... more 10/9/2014 3:28:22 PM

people

Google Protocol Buffers: find out what message is encoded in byte[]

Is there a way to find out what type of message is encoded in byte[]? I mean, is it possible to develop things like: byte [] buf; if (typeOfMessage(buf) == "AProto") { ...
Jon Skeet
people
quotationmark

No, it isn't. Several different messages could all have the same byte[] representation. A message doesn't carry around with it any type information. The wire format indicates for each tag value what the encoding of the value is out of the... more 10/9/2014 11:48:21 AM

people

Awaiting a task is taking forever or not working at all

I must be missing something, var t2 = new Task<bool>(() => { return UserName == "Admin"; }); bool x = await t2; The bool x = await t2;...
Jon Skeet
people
quotationmark

You haven't started the task. await will wait until it completes, but it's never going to complete if it doesn't get started. Perhaps you wanted Task.Run, which creates and starts a task? (I assume that in reality, your task does... more 10/9/2014 11:38:44 AM

people

Convert string to double do not respect current number decimal separator

I'm trying to convert a string representing a double from invariant culture to a double in current culture representation, I'm concerned with how to get the new double...
Jon Skeet
people
quotationmark

But the above code always gives me a double with "." as the NumberDecimalSeparator No, it returns a double. A double is just a number. It doesn't have a NumberDecimalSeparator... only a culture does, and that's only applied when... more 10/9/2014 11:14:03 AM

people

Is it possible to change if statement syntax in C#?

I'm new at C# Programming, I just wanna ask if is it possible to change if, else statement syntax in C#? Example: Instead of : a=5; b=6; if (a<b) { ...
Jon Skeet
people
quotationmark

No, you can't change the syntax of C#. It's baked into the language. Of course, you could check out the Roslyn compiler and build your own "not quite C#" compiler... but I'd strongly advise against it. Why would you want to create a... more 10/9/2014 11:04:25 AM

people

Using Take() with bindingsource

How to use Take() with the following code? var context = new Entities(); BindingSource bi = new BindingSource(); var TableName = cboSelectTable.Text.ToString(); bi.DataSource...
Jon Skeet
people
quotationmark

You'd need to cast the result of GetValue() to something appropriate. Is it always going to be a sequence of some class type? If so, as of C# 4 and .NET 4 you could use generic covariance: var context = new Entities(); var tableName =... more 10/9/2014 10:46:17 AM

people