c#-7.0

Why is the “is”-operator in an if a longer scope then the if?

拥有回忆 提交于 2021-02-10 08:17:54
问题 So my question is: Why (and maybe how to avoid it) has the Is-operator in C# a longer lifetime as used in an if? Example: Animal a = new Cat(); if (a is Cat c) { Console.WriteLine(c); // Works } Console.WriteLine(c); // Works too // Leads to an error because c is allready declared if (a is Cat c) { .... } What I would expect is, that because I declared the variable c within the if-condition, that it will be scoped to that if-condition, what is not true. Edit: I understand the bracket argument

Why is the “is”-operator in an if a longer scope then the if?

浪子不回头ぞ 提交于 2021-02-10 08:15:57
问题 So my question is: Why (and maybe how to avoid it) has the Is-operator in C# a longer lifetime as used in an if? Example: Animal a = new Cat(); if (a is Cat c) { Console.WriteLine(c); // Works } Console.WriteLine(c); // Works too // Leads to an error because c is allready declared if (a is Cat c) { .... } What I would expect is, that because I declared the variable c within the if-condition, that it will be scoped to that if-condition, what is not true. Edit: I understand the bracket argument

Why is the “is”-operator in an if a longer scope then the if?

元气小坏坏 提交于 2021-02-10 08:15:07
问题 So my question is: Why (and maybe how to avoid it) has the Is-operator in C# a longer lifetime as used in an if? Example: Animal a = new Cat(); if (a is Cat c) { Console.WriteLine(c); // Works } Console.WriteLine(c); // Works too // Leads to an error because c is allready declared if (a is Cat c) { .... } What I would expect is, that because I declared the variable c within the if-condition, that it will be scoped to that if-condition, what is not true. Edit: I understand the bracket argument

Why is the “is”-operator in an if a longer scope then the if?

二次信任 提交于 2021-02-10 08:15:02
问题 So my question is: Why (and maybe how to avoid it) has the Is-operator in C# a longer lifetime as used in an if? Example: Animal a = new Cat(); if (a is Cat c) { Console.WriteLine(c); // Works } Console.WriteLine(c); // Works too // Leads to an error because c is allready declared if (a is Cat c) { .... } What I would expect is, that because I declared the variable c within the if-condition, that it will be scoped to that if-condition, what is not true. Edit: I understand the bracket argument

Why is the “is”-operator in an if a longer scope then the if?

拟墨画扇 提交于 2021-02-10 08:13:03
问题 So my question is: Why (and maybe how to avoid it) has the Is-operator in C# a longer lifetime as used in an if? Example: Animal a = new Cat(); if (a is Cat c) { Console.WriteLine(c); // Works } Console.WriteLine(c); // Works too // Leads to an error because c is allready declared if (a is Cat c) { .... } What I would expect is, that because I declared the variable c within the if-condition, that it will be scoped to that if-condition, what is not true. Edit: I understand the bracket argument

C# 7.0 standalone discard confusion

喜欢而已 提交于 2021-02-07 12:23:30
问题 I would like to better understand a couple of examples involving the usage of C# 7.0 discard feature. Both of them make use of the so called stand alone discard. This is the first example confusing me, which is available here: public class EmailController { public ActionResult SendEmail(string email) { var correlationId = HttpContext.Request.Headers["x-correlation-id"].ToString(); // Starts sending an email, but doesn't wait for it to complete _ = SendEmailCore(correlationId); return View();

Why it's not possible to chain ref returns if the inner method also accepts out params in C#7?

…衆ロ難τιáo~ 提交于 2021-02-06 10:51:34
问题 I was experimenting with the new C#7 features and I found something strange. Given the following simplified scenario: public struct Command { } public class CommandBuffer { private Command[] commands = new Command[1024]; private int count; public ref Command GetNextCommand() { return ref commands[count++]; } public ref Command GetNextCommand(out int index) { index = count++; return ref commands[index]; } } public class BufferWrapper { private CommandBuffer cb = new CommandBuffer(); // this

Why it's not possible to chain ref returns if the inner method also accepts out params in C#7?

孤街浪徒 提交于 2021-02-06 10:51:25
问题 I was experimenting with the new C#7 features and I found something strange. Given the following simplified scenario: public struct Command { } public class CommandBuffer { private Command[] commands = new Command[1024]; private int count; public ref Command GetNextCommand() { return ref commands[count++]; } public ref Command GetNextCommand(out int index) { index = count++; return ref commands[index]; } } public class BufferWrapper { private CommandBuffer cb = new CommandBuffer(); // this

Why it's not possible to chain ref returns if the inner method also accepts out params in C#7?

坚强是说给别人听的谎言 提交于 2021-02-06 10:50:42
问题 I was experimenting with the new C#7 features and I found something strange. Given the following simplified scenario: public struct Command { } public class CommandBuffer { private Command[] commands = new Command[1024]; private int count; public ref Command GetNextCommand() { return ref commands[count++]; } public ref Command GetNextCommand(out int index) { index = count++; return ref commands[index]; } } public class BufferWrapper { private CommandBuffer cb = new CommandBuffer(); // this

Is there a shorthand way to convert a Tuple to a ValueTuple?

对着背影说爱祢 提交于 2021-02-05 07:56:07
问题 I have just included a DLL file into my project and am using a method which returns a List<Tuple> . I have only recently became aware of the ValueTuple datatype in C# 7 and want to use it instead of the normal Tuples which are returned. I am aware that I could loop through and do the conversion manually however I would like to avoid doing this if possible and was wondering if anyone was aware of a shorthand way to casting a Tuple to a ValueTuple? If I am being ignorant and this isn't possible