Browsing 7239 questions and answers with Jon Skeet

How to get an attribute of a property in c#?

Going direct to the point, How can I get Property's attribute, and, the type or the value of this attribute? For example an attribute like this:...
Jon Skeet
people
quotationmark

Once you've got the PropertyInfo for the property you're interested in, you just call GetCustomAttributes on it: ForeignKey[] keys = (ForeignKey[]) property.GetCustomAttributes(typeof(ForeignKeyAttribute), false); There's also the... more 10/23/2013 4:55:34 PM

people

What thread runs the code after the `await` keyword?

Let me just post a simple example: private void MyMethod() { Task task = MyAsyncMethod(); task.Wait(); } private async Task MyAsyncMethod() ...
Jon Skeet
people
quotationmark

Does a new thread get created to run //Code after await? Maybe. Maybe not. The awaitable pattern implementation for Task runs the continuation (the bit after the await expression) using the synchronization context which was "current"... more 10/23/2013 1:47:06 PM

people

Simple Method Output Process

I have a basic class with this method including public class Account { //MEMBERS private int acctNo; protected double balance; public double deposit; //...
Jon Skeet
people
quotationmark

I am getting an error saying "no overload for method 'getDeposit' takes 0 arguments". What am I doing wrong Exactly what it says. Here's your method call: Console.WriteLine(" output {0}", account.getDeposit()); ... and here's the... more 10/23/2013 6:44:08 AM

people

While Collections.shuffle() doesn't throw ConcurrentModificationException

I'm actually learning collections and exceptions and I can't understand why this works : List<Integer> intList = new...
Jon Skeet
people
quotationmark

The answer lies in the documentation - emphasis mine: (A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a... more 10/22/2013 6:19:46 PM

people

Adding method to event by casting delegate?

I come from a Java background and am currently learning c#. I understand that when one wants to subscribe a method to a event, one does it like the following: button.Click +=...
Jon Skeet
people
quotationmark

Are we casting the method to a delegate? Well, you're not casting - but you're using a method group conversion to convert a method name into a delegate. I thought the event wants a method to be subscribed to it? No, an event... more 10/22/2013 5:17:55 PM

people

datetime in c# for a web application

DateTime.UtcNow is used to get the current UTC time and DateTime.Now is used for the current local time. In the case of a web application, datetime.now will give the server time....
Jon Skeet
people
quotationmark

You pass the UTC time back to the client, and let the client do the UTC to local time conversion. Either that, or you need to make the client pass in the time zone, which is a pain in other ways. Usually it's better to stick to UTC for as... more 10/22/2013 4:37:07 PM

people

Convert IQueryable to IOrderedQueryable

I have 2 tables : Items and ItemMetrics. Items contains my items and ItemMetrics contains statistics about those items. I'm trying to sort a list of Items by the corresponding...
Jon Skeet
people
quotationmark

The problem is that an IOrderedQueryable needs to be able to apply a secondary ordering - and by the time you've projected the item/metric pair to just an item, you've lost the primary ordering. One approach would be to defer the... more 10/22/2013 3:44:33 PM

people

Optimizing the use of foreach

I have a small peice of code where for each is being used. I have optimized my code, but my boss expects me to optimize it even further more. I have no idea what further...
Jon Skeet
people
quotationmark

Well it's not really optimization, but your code would be simpler as: if (!Override || match.Groups[2].Value == OverrideFileName) { var df = new DownloadFileStruct(match.Groups[1].Value, ... more 10/22/2013 3:17:48 PM

people

SimpleDateFormat Unexpected Success with Phone Number

Accidentally passing in a phone number string into the format method of SimpleDateFormat sometimes results in a valid date being parsed. As an example, passing the number...
Jon Skeet
people
quotationmark

Your SimpleDateFormat is in "lenient" mode - which is very lenient indeed. If you use promsiedDateTimeFormat.setLenient(false); it will throw an exception as you'd expect when you try to parse the bogus data. Personally I think it... more 10/22/2013 3:01:03 PM

people

how to filter and sum using linq

what is the best LINQ approach for with no performance loss? how could we do the same thing using LINQ iterating the list only once? class Employee { string name ...; ...
Jon Skeet
people
quotationmark

(I'm assuming you want the filtered list as well - the other answers given so far only end up with the sum, not the list as well. They're absolutely fine if you don't need the list, but they're not equivalent to your original code.) Well... more 10/22/2013 2:43:52 PM

people