Browsing 15589 questions and answers with Jon Skeet

How to make a 00:00 timer in c#?

I'm just trying to make a basic mm:ss timer for a little sports scoreboard. I currently have int i = 0; private void matchTimer_Tick(object sender, EventArgs e) { i++; ...
Jon Skeet
people
quotationmark

You have more problems than you think. You're currently relying on the timer ticking exactly once per second. Don't do that - instead, use a System.Diagnostics.Stopwatch to measure the elapsed time, and then update the display by... more 4/24/2017 7:45:05 AM

people

Difference between Array.CreateInstance and using the new operator to create new array instance

I can see following two ways of instantiating an int array in C#: Through an API in System.Array abstract class: var arrayInstance = Array.CreateInstance(typeof(int),...
Jon Skeet
people
quotationmark

They definitely create the same kind of value - unlike if you call Array.CreateInstance and create an array with a non-zero lower bound, for example. However, they're not the same in terms of IL - the first is simply a method call, the... more 4/24/2017 6:18:56 AM

people

Is it possible to get the string of the name of a property in its get and set?

I want to store and retrieve my configs from database. I have written two methods setConfig(“configName”, value) and getConfig(“configName”) and I use them in my...
Jon Skeet
people
quotationmark

You can write a method to use the caller-information attributes: // Put this anywhere public static string GetCallerName([CallerMemberName] name = null) => name; Importantly, when you call this, don't supply an argument: let the... more 4/23/2017 11:57:41 AM

people

How to format java.util.Date with DateTimeFormatter portable?

How to format java.util.Date with DateTimeFormatter portable? I can't use Date in = readMyDateFrom3rdPartySource(); LocalDateTime ldt = LocalDateTime.ofInstant(in.toInstant(),...
Jon Skeet
people
quotationmark

TL;DR: You're right to be concerned about the use of the system local time zone, but you should have been concerned earlier in the process, when you used the system local time zone to construct a Date in the first place. If you just want... more 4/22/2017 6:36:01 AM

people

Converting google time zone to .net timezone

I'm using this : string url = string.Format("https://maps.googleapis.com/maps/api/timezone/json?location={0},{1}&timestamp=1374868635&sensor=false", lat, lon); using...
Jon Skeet
people
quotationmark

Instead of using timeZoneName, I'd suggest using timeZoneId, which will be an IANA ID such as Europe/London. You then have various options: Find a mapping from that to Windows time zone IDs yourself, e.g. with TimeZoneConverter Use Noda... more 4/21/2017 1:48:04 PM

people

What is the replacement of CryptoConfig class in .NETCore?

Currently i am doing this in my UWP app byte[] bytes = new UTF8Encoding().GetBytes(Password); byte[] hash =...
Jon Skeet
people
quotationmark

It looks like you don't need CryptoConfig at all. You just need MD5: using (var md5 = MD5.Create()) { var hash = md5.ComputeHash(Encoding.UTF8.GetBytes(password)); return BitConverter.ToString(hash); } The MD5 class is present... more 4/21/2017 1:12:36 PM

people

JSON to Dictionary

Am looking to convert a JSON string (as follows) to a dictionary. [{'Key':'superuser','Value':'s'}] Ideally, I would like to convert it a way that Dictionary[0] will be...
Jon Skeet
people
quotationmark

You're currently deserializing it as a list of dictionaries, when you only actually want a single dictionary. There may be a cleaner way of doing this, but you can deserialize it as a list of key-value pairs, then convert that into a... more 4/21/2017 10:38:24 AM

people

.NET Framework: Get Type from TypeInfo

The new reflection API introduces the TypeInfo class: https://msdn.microsoft.com/en-us/library/system.reflection.typeinfo(v=vs.110).aspx I can get a TypeInfo instance of a Type...
Jon Skeet
people
quotationmark

If you call typeInfo.GetType(), you will indeed get the execution-time type of the objec that typeInfo refers to - so some concrete type derived from TypeInfo. You want TypeInfo.AsType(): Returns the current type as a Type... more 4/20/2017 3:15:23 PM

people

Does this function call itself or call the overload?

Consider two overloads: public void add(Integer value) { add(value == null ? null : value.doubleValue()); } and public void add(Double value) { // some code...
Jon Skeet
people
quotationmark

Yes, it's well defined that it will call the Double overload. It couldn't call the Integer overload because there's no implicit conversion from double (which is the type of the conditional expression) to Integer. Basically, there are two... more 4/19/2017 3:35:29 PM

people

Why is it a valid JSON?

So, I am wondering, why does Json.NET consider this string to be valid JSON? {message:'Welcome to real world'} It's not valid according to RFC 7159. Sample code that doesn't...
Jon Skeet
people
quotationmark

Json.NET is just being lenient. It's not valid JSON, for two reasons: The member name isn't quoted The string uses single quotes instead of double quotes I don't think there's any way of getting Json.NET to be absolutely strict about... more 4/19/2017 7:25:38 AM

people