question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Out var scope vs extension methods

See original GitHub issue
  1. Case: user-defined literals requested. Answer: use extension methods #4971.
  2. 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:closed
  • Created 7 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
svickcommented, Oct 24, 2016

BTW, you can simplify Guard.IsNumber to:

public static void IsNumber(string arg, out int parsed)
{
    if (!int.TryParse(arg, out parsed)) // not "out int parsed"
        throw new ArgumentException();
}
0reactions
CyrusNajmabadicommented, Nov 8, 2022

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found