Browsing 16054 questions and answers with Jon Skeet

Why can't C# infer the type of a DataTable Row

I am trying to iterate over a DataTable and get the values from a particular column. So far I just have the Skeleton of the for loop. foreach (var row in currentTable.Rows) { ...
Jon Skeet
people
quotationmark

Why can't C# infer the type of row as it would if I was trying to iterate over a string[] for example? TL;DR: DataTable predates generics :( DataTable.Rows is declared to return DataRowCollection, which derives from... more 2/8/2018 10:08:12 AM

people

Cannot implicitly convert type void to List

I have below class structure: class Child { public List<ParentData> ParentData { get; set; } } class ParentData { public string FatherName {get;set;} public...
Jon Skeet
people
quotationmark

So this part is the problem: GrandParentData=new List<GrandParentData().Add( new GrandParentData { GrandFatherName = "",GrandMotherName =""}); There are three problems here: You're not closing the type argument You've got a... more 2/8/2018 8:08:52 AM

people

Nodatime Parsing Instant with InstantPattern fails

I use NodaTime, Version=2.2.3.0 I have set up a patter to match various Instant patterns The last pattern should match the examples below. IPattern<Instant> pattern = new...
Jon Skeet
people
quotationmark

Firstly, I don't think you actually need that many patterns - I suspect you don't need both M and MM versions, for example. But the problem is that you're using HH with tt. HH means "24-hour clock", so a value of 12 means 12pm... but then... more 2/8/2018 7:44:29 AM

people

Check IEnumerable using Contains() in a view

I'm trying to render a list of links, and the icon should change depending on wether the item Id is found within an IEnumerable or not. This is the relevant part of my view so...
Jon Skeet
people
quotationmark

The problem is that the Contains LINQ method doesn't have the signature you expect - you're trying to check whether an IEnumerable<FrontPageProduct> contains an int... it can't, because it only has FrontPageProduct references. I... more 2/7/2018 9:51:20 AM

people

Why compilation depends on how exception is thrown within instance initializer?

Why the first code snippet fails to compile (with compile error "Initializer does not complete normally") while the second compiles fine? The only difference is in the way how...
Jon Skeet
people
quotationmark

The JLS is pretty clear about this, in section 8.6: It is a compile-time error if an instance initializer cannot complete normally (ยง14.21). In your first example, the instance initializer can't complete normally. In the second... more 2/6/2018 2:41:32 PM

people

Hash String does not equal to Copied Hash String from console C#

I created hash. Than I printed it in console. Copied hash value and put it to code for comparsion. But it yield that they are not same. String input = "Hello"; ...
Jon Skeet
people
quotationmark

The result of a hash isn't UTF-8-encoded text, and shouldn't be treated that way. Convert it to hex or base64. For example: string computedHashString = Convert.ToBase64String(computedHash); Fundamentally, you need to treat data... more 2/6/2018 2:24:52 PM

people

Error parsing xml syntax error

I have the following code below. I am getting an error "the character ':' hexadecimal value 0x3A cannot be included in a name" Can anyone tell me how to fix this? Thanks Below is...
Jon Skeet
people
quotationmark

Basically, that's not how you handle namespaces in LINQ to XML. You never specify a string with a colon in - instead, you build up an XName from an XNamespace and a string. The good news is that LINQ to XML handling of namespaces is... more 2/2/2018 2:37:15 PM

people

declare variables in argument list

It's possible in c# 7 to declare variables for out variables in argument list: if (int.TryParse(input, out int result)) WriteLine(result); Is it possible to declare ("non...
Jon Skeet
people
quotationmark

You can't do it in the argument list, no. You could use pattern matching for this, but I wouldn't advise it: if (FuncGetStr() is string result && !string.IsNullOrEmpty(result)) That keeps the declaration within the source code... more 2/2/2018 11:57:29 AM

people

Overloading in local methods and lambda

static void Main() { void TestLocal() => TestLocal("arg"); TestLocal("arg"); } static void TestLocal(string argument) { } In this example, local function has the same...
Jon Skeet
people
quotationmark

Why local functions hide methods? Basically it's introducing the method name into the declaration space inside the method. Within that declaration space, the name only refers to the local method... just as it does for a local... more 2/2/2018 9:30:34 AM

people

Return key for a value found in Hashtable

I know how to find a value for a corresponding key in Hashtable but how do I find what key is attached to a specific value? I wrote a function that loops through a String and...
Jon Skeet
people
quotationmark

containsValue has to iterate over all the entries in the hash table anyway - so just change your code so that you do that: for (Map.Entry<Integer, String> entry : hashtable) { if (word.equals(entry.getValue()) { // Use... more 1/31/2018 10:43:55 AM

people