Browsing 7239 questions and answers with Jon Skeet

CS0103 and CS0136 with pattern matching

I'm a little bit confused. Why from one side I become CS0103(variable does not exist) and from another side CS0136(with own words - variable already exists) and declaration of...
Jon Skeet
people
quotationmark

There are three rules at play here: The scope of a local variable is normally the entire block in which it's declared. The variables introduced via pattern matching in switch statements are slightly narrower than that, which is how... more 3/13/2018 2:11:57 PM

people

Why this SemaphoreSlim waits indefinitely?

using System; using System.Threading; using System.Threading.Tasks; namespace ConsoleApp2 { class Program { static void Main(string[] args) { ...
Jon Skeet
people
quotationmark

I create a semaphore with limit 1 and current value of 0. It should allow 1 thread to pass, right? No - the current value is 0, exactly as you wrote. Semaphore values count down when they're awaited - the count is effectively "the... more 3/10/2018 2:19:53 PM

people

How can I mask a hexadecimal int using Java?

I have an integer that contains a hexa value. I want to extract the first characters from this hexa value like it was a String value but I don't want to convert it to a...
Jon Skeet
people
quotationmark

It's important to understand that an integer is just a number. There's no difference between: int x = 0x10; int x = 16; Both end up with integers with the same value. The first is written in the source code as hex but it's still... more 3/8/2018 1:56:25 PM

people

How do I use the Async Await functions in C# correctly

I'm still having trouble understanding how to use Async methods. I have the following code in a controller. [HttpPost] public async Task<IActionResult>...
Jon Skeet
people
quotationmark

I doubt that this has anything to do with async really. Currently you're copying one stream into a MemoryStream, but then leaving the "cursor" at the end of the MemoryStream... anything trying to read from it won't see the new data. The... more 3/7/2018 9:36:25 PM

people

How to set sql parameters

I try that code for use sql parameters but didnt work and didnt return any error. How can I fix it? string sql = "SELECT * FROM "+mw.db_name+".ananmez_genel Where...
Jon Skeet
people
quotationmark

This part is the problem: Where hasta_id='@hastaid' That's not using a parameter - that's searching for rows where the value of hasta_id is exactly the string @hastaid, because you've put it in a string literal. You need to get rid of... more 3/7/2018 5:51:57 PM

people

why doesn't byte[] to string and back work as expected

I have this code: Int32 i1 = 14000000; byte[] b = BitConverter.GetBytes(i1); string s = System.Text.Encoding.UTF8.GetString(b); byte[] b2 =...
Jon Skeet
people
quotationmark

You shouldn't use Encoding.GetString to convert arbitrary binary data into a string. That method is only intended for text that has been encoded to binary data using a specific encoding. Instead, you want to use a text representation... more 3/5/2018 8:35:16 AM

people

Using XDocument.Load(xmlreader) method?

I heard normally when using XDocument's Load or Parse method the entire file loaded into memory that is why parsing large files with this method is not recommended...but what if I...
Jon Skeet
people
quotationmark

Yes, that still loads the whole file's content into an in-memory representation. It's less useful than the XElement.Load(XmlReader) method which can be really useful to load just part of a document into memory at one time. I'd view the... more 3/2/2018 9:46:33 AM

people

What is a wrapping conversion?

When you try to cast a value from a type to another incompatible type, you get the following error in C#: CS0039 Cannot convert type A to B via reference conversion, boxing...
Jon Skeet
people
quotationmark

Wrapping converts a non-nullable value type to its nullable equivalent. Unwrapping is the reverse. For example: int x = 5; int? y = x; // Wrapping int z = (int) y; // Unwrapping The C# spec doesn't actually call these "wrapping... more 2/27/2018 4:25:04 PM

people

Is there a way to indicate that a method will never return?

Assuming we have the following (very basic code) public int Foo() { while (true) { } // No "return 0" etc. needed here. } the compiler can understand that this...
Jon Skeet
people
quotationmark

Why does the compiler allow for omitting the return statement in the first case? Because the final } is unreachable. That's the condition that the compiler prevents (and that's in the specification): ever being able to reach the end... more 2/26/2018 4:17:25 PM

people

C# Error variable is unassigned in try catch method?

static void Main(string[] args) { int lengthCountdown; print tell = new print(); timer counter = new timer(); Error invalidInput = new Error(); tell.tell("Hi i...
Jon Skeet
people
quotationmark

If you don't have the try/catch block, you'd only ever reach the statement that uses lengthCountdown if the assignment had succeeded anyway. If int.Parse or Console.ReadLine() throw a FormatException, you're currently catching that... more 2/25/2018 4:00:50 PM

people