local-variables

How to declare a local constant in C#?

萝らか妹 提交于 2019-12-01 13:41:09
问题 How to declare a local constant in C# ? Like in Java, you can do the following : public void f(){ final int n = getNum(); // n declared constant } How to do the same in C# ? I tried with readonly and const but none seems to work. Any help would be greatly appreciated. Thanks. 回答1: In C#, you cannot create a constant that is retrieved from a method. Edit: dead link http://msdn.microsoft.com/en-us/library/e6w8fe1b(VS.71).aspx This doc should help: https://docs.microsoft.com/en-us/dotnet/csharp

When does a local variable inside a function *actually* gets allocated

旧街凉风 提交于 2019-12-01 08:09:25
Just curious about this. Following are two code snippets for the same function: void MyFunc1() { int i = 10; object obj = null; if(something) return; } And the other one is... void MyFunc1() { if(something) return; int i = 10; object obj = null; } Now does the second one has the benefit of NOT allocating the variables when something is true? OR the local stack variables (in current scope) are always allocated as soon as the function is called and moving the return statement to the top has no effect? A link to dotnetperls.com article says "When you call a method in your C# program, the runtime

When does a local variable inside a function *actually* gets allocated

南楼画角 提交于 2019-12-01 06:20:13
问题 Just curious about this. Following are two code snippets for the same function: void MyFunc1() { int i = 10; object obj = null; if(something) return; } And the other one is... void MyFunc1() { if(something) return; int i = 10; object obj = null; } Now does the second one has the benefit of NOT allocating the variables when something is true? OR the local stack variables (in current scope) are always allocated as soon as the function is called and moving the return statement to the top has no

Function-local, self-referential, lazy fibonacci sequence

为君一笑 提交于 2019-12-01 04:12:42
问题 I would like to create a function that returns a lazily extended infinite sequence of Fibonacci numbers. Right now, I can make my sequence available in the top-level namespace like this: (def fibonacci-numbers (lazy-cat [0 1] (map + fibonacci-numbers (rest fibonacci-numbers)))) However, this means that if I start consuming a lot of them, I lose control over the garbage collection. I am looking to do something like: (defn fibonacci-numbers-fn [] (lazy-cat [0 1] (map + (fibonacci-numbers-fn)

Java how to print all local variables?

喜欢而已 提交于 2019-12-01 03:57:26
I have a method and want to examine variables inside it without debugging - is it possible in Java? I do not want to write tons of code like: System.out.println("a: " + a); I want to something like: System.out.printLocals(); Also it should be great to have something like: System.out.printMembersOf(someObjectInstance); Well, you can write a method with a varargs parameter and just write: dump(variable1, variable2, variable3, variable4, ...); It's not ideal, but it will be enough in some circumstances. There's no way to automatically grab all the local variables from a method and dump them

“array initializer needs an explicit target-type” - why?

て烟熏妆下的殇ゞ 提交于 2019-12-01 02:42:18
Following JEP 286: Local-Variable Type Inference description I am wondering, what the reason is for introducing such a restriction, as: Main.java:199: error: cannot infer type for local variable k var k = { 1 , 2 }; ^ (array initializer needs an explicit target-type) So for me logically it should be: var k = {1, 2}; // Infers int[] var l = {1, 2L, 3}; // Infers long[] Because Java compiler can already infer properly the type of an array: void decide() { arr(1, 2, 3); // call void arr(int ...arr) arr(1, 2L, 3); // call void arr(long ...arr) } void arr(int ...arr) { } void arr(long ...arr) { }

Java how to print all local variables?

旧街凉风 提交于 2019-12-01 01:22:54
问题 I have a method and want to examine variables inside it without debugging - is it possible in Java? I do not want to write tons of code like: System.out.println("a: " + a); I want to something like: System.out.printLocals(); Also it should be great to have something like: System.out.printMembersOf(someObjectInstance); 回答1: Well, you can write a method with a varargs parameter and just write: dump(variable1, variable2, variable3, variable4, ...); It's not ideal, but it will be enough in some

How to default-initialize local variables of built-in types in C++?

旧时模样 提交于 2019-12-01 00:18:06
How do I default-initialize a local variable of primitive type in C++? For example if a have a typedef: typedef unsigned char boolean;//that's Microsoft RPC runtime typedef I'd like to change the following line: boolean variable = 0; //initialize to some value to ensure reproduceable behavior retrieveValue( &variable ); // do actual job into something that would automagically default-initialize the variable - I don't need to assign a specific value to it, but instead I only need it to be intialized to the same value each time the program runs - the same stuff as with a constructor initializer

Why doesn't Perl's foreach require its variable to be declared with my?

情到浓时终转凉″ 提交于 2019-11-30 17:53:24
Today, I stumbled over something in Perl I was not aware of: it "localizes" the variable that the elements of the list iterated over is assigned to. This, of course, is documented in the Perl documentation - however I failed to remember or read it. The following script demonstrates what I mean: use warnings; use strict; my $g = 99; foreach $g (1..5) { p($g); } sub p { my $l = shift; printf ("%2d %2d\n", $g, $l); } The script prints 99 1 99 2 99 3 99 4 99 5 because $g is "localized" to the foreach loop. As far as I can tell there is no difference if I had added my to $g in the foreach loop:

Why doesn't Perl's foreach require its variable to be declared with my?

落花浮王杯 提交于 2019-11-30 16:51:46
问题 Today, I stumbled over something in Perl I was not aware of: it "localizes" the variable that the elements of the list iterated over is assigned to. This, of course, is documented in the Perl documentation - however I failed to remember or read it. The following script demonstrates what I mean: use warnings; use strict; my $g = 99; foreach $g (1..5) { p($g); } sub p { my $l = shift; printf ("%2d %2d\n", $g, $l); } The script prints 99 1 99 2 99 3 99 4 99 5 because $g is "localized" to the