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
2
votes
1 answer

How to call a function recursively if it contains a local function with the same name?

I was playing around the local functions and could not figure out how to call the host function if it contains the local function with the same name. class Program { static void Main(string[] args) { new Test().Foo(); …
user9932489
1
vote
1 answer

Why can I write local functions in .NET Framework 4.6.1.?

Local functions were introduced in C# 7.0 and C# 7.0 came in .NET Framework version 4.7. I'm absolutely certain that I'm running .NET Framework 4.6.1., but I can write and use local function without issue. How can this be explained?
J. Mini
  • 1,868
  • 1
  • 9
  • 38
1
vote
0 answers

Why does creating a new object inside local function results in a faster runtime?

hello I'm currently doing some LeetCode exercises and I'm failing to understand why I have a faster runtime on the first code sample than the second on this reverse list problem. the linked list is like so: /** * Definition for singly-linked list. …
Richard
  • 481
  • 1
  • 3
  • 11
1
vote
1 answer

Lua `local a = {b=func1}`

I am new to lua and encountered the following piece of code function X() local function Y() ... end local var1 = {var2=Y} ... return blah end What does local var1 = {var2=Y} do/mean here? Thanks!
Sarwagya
  • 166
  • 11
1
vote
2 answers

Can you use a ternary operator in a C# local function?

Here is what I would like to implement: public static void foo() { // Local function int? parseAppliedAmountTernary( decimal? d ) { d.HasValue ? return Convert.ToInt32( Math.Round(d.Value, 0) ) : return null; } // The…
Vince I
  • 570
  • 5
  • 22
1
vote
1 answer

dart: why local functions can not call each other?

// OK, it works! void global_1() => global_2(); void global_2() => global_1(); void main() { // ERROR Local variable 'local_2' can't be referenced before it is declared. void local_1() => local_2(); // <=== ERROR void local_2() =>…
damphat
  • 18,246
  • 8
  • 45
  • 59
1
vote
2 answers

Can you somehow use a local function on a global level in python?

I'm writing a program and I'm using espeak library to make it speak. The thing is I've already written my program but now I want to make a way to ask the user in the beginning if he wants the program to talk to him or just rely on reading. So if he…
1
vote
2 answers

permutations-why do we need to return copy of the list from local function?

I'm writing a permutation function as below, what I'm confused is if we return "res.append(nums[:])", it works perfectly, but if we return just "res.append(nums)", it'll only return the original nums value, without storing the new nums after my…
1
vote
0 answers

How can I notify ReSharper that the return value of a local function is NotNull / CanBeNull?

Is there any way to notify ReSharper that the return value of a local function is NotNull or CanBeNull? Otherwise I need to convert local functions into member functions or turn off the pessimistic nullability checking because they produces tons of…
terborac
  • 323
  • 2
  • 13
1
vote
0 answers

New instances of local function is created everytime enclosing function is called?

A Python tutorial I came across mentions that new instances of local function are created every time the enclosing function is called. I tried to test that out with below two examples: Example 1: I am fetching object id of local function from within…
vajravelu
  • 53
  • 1
  • 8
1
vote
0 answers

Are local functions in properties a good idea or a sign the property should be it's own method?

If I did something like this: public int Foo { get { return GetFoo() + 5; int GetFoo() => 3; } } It will actually compile, which makes me wonder if this would be a good or bad practice. Now, clearly this particular…
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
0
votes
1 answer

Local extension method

Sometimes it becomes necessary to write some helper that is applicable only in the code of some method. I've tried to write extension method locally (as in static local functions), but the program does not compile. The minimal repeatable…
Reverin
  • 49
  • 8
0
votes
2 answers

Why do I need to qualify a call to a static method inside a local function having the same name?

I have a class with a static method, like so: public class Calculator { public static decimal Calculate(decimal op1, decimal op2) => op1 * op2; } I add an instance method to that class which calls that static method: public decimal Foo(decimal…
0
votes
1 answer

How to use Continue For within an inner function in VB.NET

I have an outer function which contains a loop and an inner function where I'm trying to use Continue For. How can I use the Continue For within the inner function. For example: Public Function Create(variable1 As String, variable2 As String) As…
Rich
  • 6,470
  • 15
  • 32
  • 53
0
votes
1 answer

Is there an EASY way to simulate local functions?

I've got a member-function that need to access both member data and "local" data: struct S final { int i = 123; // ... a bunch of legacy code ... void f() { // ... a bunch of legacy code ... double d = 1.23; // ... a…
Jack Brown
  • 63
  • 6