Out var scope vs extension methods
See original GitHub issue- Case: user-defined literals requested. Answer: use extension methods #4971.
- Case: inconsistent scoping suggested for
if-else
#12597, #12939. Answer: why not extension methods?
public void Before(string arg)
{
if (!int.TryParse(arg, out int number))
throw new ArgumentException();
Console.WriteLine($"It's a number: {number}");
}
public void After(string arg)
{
Guard.IsNumber(arg, out int number);
Console.WriteLine($"It's a number: {number}");
}
public static class Guard
{
public static void IsNumber(string arg, out int parsed)
{
if (!int.TryParse(arg, out int secondParsed))
throw new ArgumentException();
parsed = secondParsed;//Otherwise: "error CS0136: A local or parameter named 'parsed' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter."
}
}
Issue Analytics
- State:
- Created 7 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Variable Scope For an Extension Method
When the extension method accessed the object, it's the same object as outside the method. Any change to the property made using the...
Read more >Extension Methods - C# Programming Guide
Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive. The following example ......
Read more >out parameter modifier - C# Reference
To use an out parameter, both the method definition and the ... The out keyword cannot be used on the first argument of...
Read more >When to write extension methods for your own classes?
Extension methods have one more benefit: they can still be used as a static method, and can be passed directly as a value...
Read more >Extension Method in C#. Everything You Need To Learn
Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive. Create a Class ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
BTW, you can simplify
Guard.IsNumber
to:Closing this out. We’re doing all language design now at dotnet/csharplang. If you’re still interested in this idea let us know and we can migrate this over to a discussion in that repo. Thanks!