This question already has answers here:
c# pass by value
(3 answers)
Closed 2 years ago.
I am confused about how the code below works.
Why is the output not 10 but 5?
public class Program
{
public void MyFunc(int x){
x = 10;
}
public void Main()
{
int x = 5;
MyFunc(x);
Console.WriteLine(x);
}
}
Thanks a lot.
Read about the "ref keyword"
Now result is 10
public static void MyFunc(ref int x)
{
x = 10;
}
public static void Main()
{
int x = 5;
MyFunc(ref x);
Console.WriteLine(x);
}
Related
This question already has answers here:
Compiler Error CS0120 [duplicate]
(2 answers)
Closed 2 months ago.
I am new to C#, sorry if this is duplicate. I am simply trying to add two variables together but I get the error from the title
An object reference is required for the non-static field, method, or property 'Program.x'
Here is code
using System;
namespace HelloWorld
{
class Program
{
int x = 3;
int y = 10;
static void Main(string[] args)
{
int mathResults = x + y;
string results = mathResults.ToString();
Console.WriteLine("Hello World!");
}
}
}
Could someone please explain WHY I am getting this error? Thank you!
As your Main() method is static, you must use static variables in it.
So it should be like this :
class Program
{
static int x = 3;
static int y = 10;
static void Main(string[] args)
{
int mathResults = x + y;
string results = mathResults.ToString();
Console.WriteLine("Hello World!");
}
}
This question already has an answer here:
Get Reference to Field from Reflection [duplicate]
(1 answer)
Closed 2 years ago.
I need to get a reference to a field value in C# but I cant find any way to get an actual reference to the field that can be passed as a parameter. Any help will be greatly appreciated.
Example:
public class SomeClass
{
public int value = 0;
}
public class SomeOtherClass
{
static void Main(string[] args)
{
SomeClass x = new SomeClass();
Console.WriteLine(x.value); //Outputs "0"
foreach (FieldInfo field in x.GetType().GetFields())
{
Log(ref field.value) //Outputs "10"
} // ^
} //Not a valid member
static void Log(ref int i)
{
i = 10;
Console.WriteLine(i);
}
}
Try the below
public class SomeClass
{
public int value = 0;
}
public class SomeOtherClass
{
public static void Main(string[] args)
{
SomeClass x = new SomeClass();
Console.WriteLine(x.value); // Output Line 1
foreach (FieldInfo field in x.GetType().GetFields())
{
Console.WriteLine(field.GetValue(x)); // Output Line 2
var y = (int)field.GetValue(x);
Log(ref y);
Console.WriteLine(y); // Output Line 4
}
}
static void Log(ref int i)
{
i = 10;
Console.WriteLine(i); // Output Line 3
}
}
Output:
0
0
10
10
class Program
{
public class SomeClass
{
public int value = 0;
}
public class SomeOtherClass
{
static void Main(string[] args)
{
var x = new SomeClass();
Console.WriteLine(x.value); //Outputs "0"
foreach (FieldInfo field in x.GetType().GetFields())
{
var value = (int)field.GetValue(x);
Log(ref x);
Console.WriteLine(x.value);
}
}
static void Log(ref SomeClass x)
{
x.value = 10;
}
}
}
This question already has answers here:
Passing Objects By Reference or Value in C#
(9 answers)
Closed 4 years ago.
private void assign(int num)
{
num = 10;
{
private void doSomething()
{
num = 0;
assign(num);
MessageBox.Show(num.ToString());
}
I get the answer 0 and not 10. can someone explain how this happens? my objective is to modify the variable.
As from comments you have 2 options, ref and out
1)
private void assign(ref int num)
{
num = 10;
}
private void doSomething()
{
int num = 0;
assign(ref num);
MessageBox.Show(num.ToString());
}
2)
private int assign(out int num)
{
num = 10;
}
private void doSomething()
{
var num = 0;
assign(out num);
MessageBox.Show(num.ToString());
}
Normally this is done by returning the value:
private int assign(int input)
{
//some complicated calculation on input.
return 10;
}
private void doSomething()
{
var num = 0;
num = assign();
MessageBox.Show(num.ToString());
}
This question already has an answer here:
C# - Why is this variable not being changed after being put through a method [duplicate]
(1 answer)
Closed 5 years ago.
Here is the block of code that I am having a bit of trouble with:
using System;
namespace TestProgram {
class Test {
static void Main() {
int number = 10;
MultiplyByTen(number);
Console.WriteLine(number);
Console.ReadKey(true);
}
static public void MultiplyByTen(int num) {
num *= 10;
}
}
}
When I run this block of code, I got 10 as the output instead of 100. My question is: Why does this happen and how to solve it?
Thanks for the help.
The problem is that when the variable number enters into the method MultiplyByTen the value is copied and the variable you are modifying inside it's in fact the copy, so, the original was not changed.
Try this instead:
public static void MultiplyByTen(ref int num)
{
num *= 10;
}
But remember you will have to call it with the ref keyword as well.
static void Main()
{
int number = 10;
MultiplyByTen(ref number);//Notice the ref keyword here
Console.WriteLine(number);
Console.ReadKey(true);
}
I recommend you to also check this out: Passing Objects By Reference or Value in C#
You need to return the value back to the function also assign the returned value to the number.
static void Main()
{
int number = 10;
number = MultiplyByTen(number);
Console.WriteLine(number);
Console.ReadKey(true);
}
static public int MultiplyByTen(int num)
{
return num *= 10;
}
Using your implementation:
using System;
namespace TestProgram {
class Test {
static void Main() {
int number = 10;
//MultiplyByTen(number);
//Console.WriteLine(number);
Console.WriteLine(MultiplyByTen(number));
Console.ReadKey(true);
}
static public int MultiplyByTen(int num) {
return num *= 10;
}
}
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is a Java static block equivalent to a C# static constructor?
Is there an equivalent to:
public class people {
private static int x;
//...
static {
x = 3;
}
}
of JAVA in C#.NET?
Yeah, it looks mostly the same
public class People
{
private static int x;
static People()
{
x = 3;
}
}
but you could also do this:
public class People
{
private static int x = 3;
}
you can use a static constructor
static people()
{
x= 3;
}
see http://msdn.microsoft.com/en-us/library/k9x6w0hc(v=vs.80).aspx
or you can just initialise it as-is
private static int x = 3;