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

Python - Is using a local closure to handle exceptions in a list comprehension duplicating a library function or otherwise bad Python practice?

I have a function that takes an indexed pandas.Series of things and a dataframe of stuff that I want to use the things on group by group. It is common for the dataframe to contain groups for which there is no matching thing, so a simple list…
jtolle
  • 7,023
  • 2
  • 28
  • 50
0
votes
1 answer

Is there a way to make a derived instance use an existing base instance

For a class hierarchy like: struct base { int i = 1; }; struct derived final : public base { int j = 2; void f() { i = 2; } }; // more derivations from base I'd like a way to create an instance of derived but using an existing base…
user16538449
0
votes
1 answer

Understanding Local Function Capturing of Variables inside Loops

I've noticed the following. This C# code: List methodList = new List(); for (int iFn = 0; iFn < 4; ++iFn) { void thisLocalFunction() { string output = iFn.ToString(); …
GarrickW
  • 2,181
  • 5
  • 31
  • 38
0
votes
1 answer

.net core switch expression incrementing with wrong value

I was playing around with my intcode computer implementation (from advent of code 2019) and found that when I implemented the switch (to select which operation to execute) it was taking the wrong value. The following code demonstrates…
subalary
  • 43
  • 3
0
votes
0 answers

Style and performance of declaring a C# local function inside a codeblock, for example a foreach?

I just discovered that one can, in C#, declare a local function not just at the top level scope of the method but also in nested scopes in the method. I was wondering if this was widely know, I searched and didn't see it mentioned any where. What…
-1
votes
2 answers

How to use base method's local function's in overriden method

If I had this code: public virtual void Foo() { void Bar() { // do important stuff for Foo } } // In a child class: public override void Foo() { Bar(); // Doesn't work base.Bar(); // Also doesn't work } Is…
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
-5
votes
3 answers

Error when calling a function in C# because variable is used in parameter

I'm new to c#, but I'm trying to create a basic program that can use a function that adds to numbers (this is just practice I know it's inefficient. { int AddNumbers(int num1, int num2) //Here's where the error comes …
1 2
3