Browsing 16016 questions and answers with Jon Skeet

Arrays first index displaying wrong element

I am currently studying C# language and getting 0 on the first index[0] where I am looking to see the user input which is "yx". Here's what I have written: using...
Jon Skeet
people
quotationmark

This loop doesn't do what the code suggests you think it does: foreach (var newemptyarray in y) That's iterating over the elements in y, which are integers. So newemptyarray isn't an array, it's just a value. On the first iteration of... more 10/23/2017 6:47:09 AM

people

How is switch variable declaration scoped?

How is following possible? switch (param.ParameterType) { case Type x when x == typeof(byte): int invalid; break; case Type x when x == typeof(short): ...
Jon Skeet
people
quotationmark

Question is, how is x scoped inside each case without any visible blocks. meanwhile, variable invalid cant be declared in different switch cases. it has to be inside a block. Variables introduced via pattern matching in case labels... more 10/22/2017 5:56:52 PM

people

Use of await keyword in c#

I have a doubt. I have read that await keyword is used when we want to wait for a particular function to finish the operation. public async void Work() { await...
Jon Skeet
people
quotationmark

But I have a doubt that even if the await and async keyword are not used then also Work() method is going to wait for SlowTask() method to complete its execution before printing "Execution completed" because it will be executed line by... more 10/22/2017 8:55:43 AM

people

How to mark a C# assembly as a CLS Compliant when using new csproj format?

I recently upgraded a C# project from xproj format to the new csproj. During the migration, the old AssemblyInfo.cs has been removed so I'm wondering how I can indicate that my...
Jon Skeet
people
quotationmark

Just add AssemblyInfo.cs back, with only the parts that aren't autogenerated. There's nothing to stop you from applying assembly attributes in code - it's just that most of the common attributes are provided from the project settings. more 10/21/2017 8:14:23 PM

people

Different property value for contracts

I have two interfaces implemented by one main class. How can i refactor my code in a way that on implementing each contract, the methods of each contract has a different value for...
Jon Skeet
people
quotationmark

It sounds like all you need is explicit interface implementation: public class Class1 : Interface1, Interface2 { // Note the lack of access modifier here. That's important! Data Interface1.GetData() { // Implementation... more 10/20/2017 6:29:56 AM

people

Why can't the type conversion of `this` be implicitly inferred from the generic contraint?

I have the following class: public class Item<TItem> where TItem : Item<TItem> { void GetReference() { TItem item = this; } } Here TItem...
Jon Skeet
people
quotationmark

Because it wouldn't be safe. Consider: public class GoodItem : Item<GoodItem> { // No problem } public class EvilItem : Item<GoodItem> { // GetReference body would be equivalent to // GoodItem item = this; //... more 10/19/2017 4:59:16 PM

people

How can one calculate the Nth non holiday from a given date?

Let's say I have a List<DateTime> L that contains holidays dates in the US. Let DateTime d be today 2017/10/19. I want to calculate the next n-th non-holiday date (where n...
Jon Skeet
people
quotationmark

If the set of holidays is finite (and reasonably small) It's really easy to create an infinite sequence of dates: static IEnumerable<DateTime> GetDates(DateTime start) { DateTime current = start; while (true) { ... more 10/19/2017 3:13:14 PM

people

Unpacking a BIG string with gzip

I'm facing a big problem, I have to unzip a BIG string, however I can not divide it to unpack, and when I try to unzip it total, it only unpacks a piece. Link to the compressed...
Jon Skeet
people
quotationmark

I would try to stream the operation - you don't need everything in memory all at one time. You can set your GZipStream to write straight to your output file, then read a chunk of your original text file in at a time, convert the hex to... more 10/19/2017 1:28:22 PM

people

Bigdecimal Not giving exact output in Java

Im adding three big decimals here, but it should give me accurate answer. I'm having two strings here and then converting to big decimal. Please dont ask why Im using strings....
Jon Skeet
people
quotationmark

The problem is your use of new BigDecimal(100.05). The value of a is then 100.0499999999999971578290569595992565155029296875. If you had specified that value as a string instead, all would be well: BigDecimal a = new... more 10/19/2017 10:45:29 AM

people

running a c# .net Console Application multiple time with different variables

I'm trying to automate a task. I have to repeat the task 14 times and want my application to do it for me. my application runs once without a problem. Can anyone explain how I do...
Jon Skeet
people
quotationmark

I'd suggest passing an argument into the Main method which is a file containing all the URLs to fetch. You can then read all those URLs into a list or array (e.g. with File.ReadAllLines, and infer the output file from the URL. Move most of... more 10/18/2017 6:16:40 PM

people