Browsing 7239 questions and answers with Jon Skeet

Collect indexes from list

Is there a linq function in c# which enables you to collect IEnumerables from a specific range of indexes? An example would be var objectArray = new string[] { "Bill",...
Jon Skeet
people
quotationmark

If the original datasource is already accessible by index, such as for a list or an array, you can just use indexArray.Select as Matt showed. If you've got an IEnumerable<T> instead, you can use the Where overload which provides the... more 4/24/2017 4:45:01 PM

people

Is the Left Hand Operand First broken?

according to this here left will be evaluated before right... but I have a project where: int[] df = null; //GetDataFrame() int xIndex = 12; //GetLearningIndex() df[0] = 1 %...
Jon Skeet
people
quotationmark

There's some confusion here... parts of the LHS of the assignment operator are evaluated first. In particular, the expressions df and 0 will be evaluated before GetLearningIndex, but the array element assignment (including index... more 4/24/2017 8:12:35 AM

people

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