Questions tagged [local-functions]

Local Function in C#

The local function feature is introduced in C# 7.0. It allows you to declare a method inside the body of an already defined method. Or in other words, we can say that a local function is a private function of a function whose scope is limited to that function in which it is created. The type of local function is similar to the type of function in which it is defined. You can only call the local function from their container members.

37 questions
26
votes
4 answers

Why declare a local function static in C# 8.0

In C# 8.0, Static Local Functions are announced Can anyone help enlighten me as to why you would want to declare a local function as static? The reason given in in the article: to ensure that local function doesn't capture (reference) any variables…
fourbeatcoder
  • 1,159
  • 3
  • 13
  • 21
22
votes
4 answers

"Variable $foo will not stay shared" Warning/Error in Perl While Calling Subroutine

Update3: If you like this posting please don't upvote me but upvote the genius answer by DVK below. I have the following subroutines: use warnings; #Input my @pairs = ( "fred bill", "hello bye", "hello fred", "foo bar", "fred…
neversaint
  • 60,904
  • 137
  • 310
  • 477
17
votes
1 answer

Set readonly fields in a constructor local function c#

The following does not compile. public class A { private readonly int i; public A() { void SetI() { i = 10; } SetI(); } } It fails with this error: CS0191 A readonly field cannot be…
ilias
  • 2,620
  • 2
  • 27
  • 37
15
votes
1 answer

With c# why are 'in' parameters not usable in local functions?

For example, public int DoSomething(in SomeType something){ int local(){ return something.anInt; } return local(); } Why does the compiler issue an error that the something variable cannot be used in the local function?
Frank
  • 903
  • 7
  • 14
10
votes
1 answer

Why is better to use a local function instead of just inlining the code?

I've found this code in here: public static IEnumerable DistinctBy(this IEnumerable source, Func keySelector, IEqualityComparer? comparer) { if (source == null) throw new…
Tao Gómez Gil
  • 2,228
  • 20
  • 36
6
votes
1 answer

Any one know how exactly IL/CLR generates Local Functions C#7

https://www.infoworld.com/article/3182416/c-7-in-depth-exploring-local-functions.html I really need to know exactly following code how Local Functions supposed to be. Am 0% IL Code/MSIL experience. public static void Display(string str) { …
user15404011
4
votes
3 answers

A comparison between using labels vs helper-and-main at the top level vs nested defun's in Common Lisp. Which is the best?

I am trying to learn Common Lisp with the book Common Lisp: A gentle introduction to Symbolic Computation. In addition, I am using SBCL, Emacs, and Slime. In the advanced section of chapter 8, the author presents the labels special function.…
Pedro Delfino
  • 2,421
  • 1
  • 15
  • 30
4
votes
1 answer

Summary comments and reference of local functions is not working

As you know in C# 7.0 added some new features One of them is Local functions. I looked at some examples and use cases of using local functions and found two reasons to use them: 1) Hide function or method. The reason is: if the function had been…
Dilshod K
  • 2,924
  • 1
  • 13
  • 46
4
votes
2 answers

C# local function with closure as event handler: why it works like this?

I encountered a behavior recently that I don't understand. I have a local function that captures variables/parameters from the enclosing method. In this case it seems to me that every time I call the enclosing method, a new "instance" of the local…
Ádám Bozzay
  • 529
  • 7
  • 19
3
votes
1 answer

Can I tell the outer function to return, from inside its local function?

I have a IEnumerator and I need to do some checks inside the function and if some of those checks fail, I need to do some maintenance and then exit the IEnumerator. But when I write yield break into the inner function, it thinks I'm trying to return…
DiMarzio
  • 153
  • 1
  • 10
3
votes
1 answer

Why do I have to type-cast a strongly typed function using a dynamic input-parameter?

I have a converter method: MyPoco Convert(dynamic p) => new MyPoco { A = p.X, B = p.Y }; void Test() { dynamic item = new { X = 1, Y = 2 }; var poco = (MyPoco)Convert(item); } I have to explictly cast the result to MyPoco, otherwise poco…
Frode
  • 3,325
  • 1
  • 22
  • 32
3
votes
3 answers

Clear persistent variables in local functions from within the main function

I have a code which consists of a single file containing multiple functions, some of which use persistent variables. In order for it to work correctly, persistent variables must be empty. There are documented ways to clear persistent variables in a…
Dev-iL
  • 23,742
  • 7
  • 57
  • 99
3
votes
4 answers

c# ERROR : The modifier 'private ' is not valid for this item

It doesn't matter what modifier I put on the front of the function(I've tried with public, private and even protected), I always receive an error, the same error. The code is clean only after I delete the modifier and I left the function "Array()"…
Corina
  • 137
  • 1
  • 1
  • 12
2
votes
2 answers

How many times the code of a local function is created? Only once or anytime the parent function is called?

Modern C# supported definition of nested functions. For example: public bool Test(params int[] args) { bool isNumberValid(int i) { return i > 0; } foreach(var n in args) { if(!isNumberValid(n)) { return false; …
Arnold Zahrneinder
  • 4,788
  • 10
  • 40
  • 76
2
votes
1 answer

Where are Lua's "global" local values stored?

I need to call a Lua function from C and as long the function is global I can find it in the global table, but if it is declared local, how can I push the address on the stack to call it? function MyGlobal() print("Global") end local function…
Max Kielland
  • 5,627
  • 9
  • 60
  • 95
1
2 3