Browsing 7239 questions and answers with Jon Skeet

DateTime.Now/UtcNow Default Culture Client or Server?

I know how to specify cultures, date formats, time zones, etc... My question here is when you just type DateTime.Now or DateTime.UtcNow in C#, is it using the culture from the...
Jon Skeet
people
quotationmark

DateTime.Now and DateTime.UtcNow don't have any culture at all. It's only when you format one as a string that it will use a culture... and which culture is used will depend on how you format it. To be clear in your code, just specify the... more 8/19/2014 5:54:43 PM

people

Non Dictionary Value Value Enumerable

I need to store a <string, byte[]> pair for creating zip files (I execute an asynchronous download function that returns the byte array of the file, and I get the file name...
Jon Skeet
people
quotationmark

You could use a List<Tuple<string, byte[]>>... or personally I'd strongly consider creating a custom class for this - and then just have a list of them. The advantage of using a custom class over Tuple is that you can then... more 8/19/2014 5:13:23 PM

people

Combine overloading for different types of Dictionary into one function

I need a helper function to convert string like "1=alice;2=bob;3=charlie" into a Dictionary<int, string>, and string like "1=true;2=false;3=true" into a Dictionary<int,...
Jon Skeet
people
quotationmark

Yes, you should have a generic method instead. Personally I'd make it return a new dictionary rather than clearing an existing one, mind you... and use LINQ to implement it: private static Dictionary<int, T> Load<T>(string... more 8/19/2014 2:11:08 PM

people

Linq Queries and Cannot implicitly convert type error

According to given project id(this id is coming to action as a parameter), I want to find this project and this project's issues and then I want to find some issues which has the...
Jon Skeet
people
quotationmark

The problem is that your Issues property is already a List<Issue>. I suspect you want something like: // TODO: Fix property naming... var project = db.Projects.Single(p => p.projectId == projectId); var issues =... more 8/19/2014 10:55:23 AM

people

How to Extract current date & time from user pc TimeZone

anyone can advise that is there any way by which i can detect country wise time for UK, Germany & France. i am not sure but can we extract country wise time from datetime utc...
Jon Skeet
people
quotationmark

Do not follow that code. It's terrible code more ways than I have time to go into right now. Basically, you need to get the time zone that the user is in - which may require more than just the country, as several countries span multiple... more 8/19/2014 9:10:51 AM

people

Generic Method Resolution

Consider the following code: public class Tests { public void Test() { Assert.AreEqual("Int", DoSomething(1)); } public static string...
Jon Skeet
people
quotationmark

In your call, you're not specifying a type argument - so the compiler would have to infer the type of T. It can't do that for your second method, because the type parameter is never mentioned in the declared parameters. Therefore, that... more 8/18/2014 4:56:47 PM

people

Extension method to handle database values

I find myself reading data from various SQL objects (DataTable, SqlReader)) and assigning to my own custom objects. Often times I cannot be sure if the data I am retrieving from...
Jon Skeet
people
quotationmark

Sure, just make your method return int? instead of int. Heck, then it can be even simpler: public static int? GetIntFromDB(this DataRow row, string columnName) { return row[columnName] as int?; } I'd personally do it slightly... more 8/18/2014 4:51:16 PM

people

How to get human readable date with unix time stamp in milliseconds?

Is there a solution without using the Date class which is deprecated, and without having to specify some format like in SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"), since I just...
Jon Skeet
people
quotationmark

The Date class isn't deprecated - just many of its constructors/methods are. You can still use: String text = new Date(millisSinceEpoch).toString(); You can't change the string format - it will always use the system default time zone,... more 8/18/2014 4:36:55 PM

people

How does reflection work on resource repositories

I want to understand how such a code works to retrieve the resource of the specified ThreadUI Culture? var value = resourceAssembly .GetType("Namespace.FooBar") ...
Jon Skeet
people
quotationmark

If you open up the generated C# file corresponding to that type, you'll see something like this: internal static string Hello { get { return ResourceManager.GetString("Hello", resourceCulture); } } Unless you specific... more 8/18/2014 4:17:04 PM

people

Trying to add data to my List<book> but getting "No overload for method takes 2 arguements". What can I do?

I am making a bookshelf application and I am having an issue trying to add data to my list. Here is the code that I am having trouble with: static List<Book> book = new...
Jon Skeet
people
quotationmark

You want to add a Book to your List<Book>... but you're not actually creating a Book instance. You probably want: Book book = new Book(bookTitle, bookAuthor); books.Add(book); ... that's after you've changed the name of your... more 8/18/2014 2:38:24 PM

people