This question already has answers here:
Can I modify a passed method parameter
(3 answers)
Closed 4 years ago.
Why c = null does not work in the following code?
public class Sample{
public int c;
}
public class Program
{
public static void Main(string[] args)
{
Sample c = new Sample();
Console.WriteLine(c.c);
f(c);
Console.WriteLine(c.c);
g(c);
Console.WriteLine(c.c);
}
static void f(Sample c){
c.c = 2;
}
static void g(Sample c){
c = null;
}
}
The output is 0, 2 and 2, while I expect the null for the last output.
Its because reference to Sample is copied so null is assigned to the copied reference and original reference does not changed.
if you want to change original reference you should pass it by reference:
static void g(ref Sample c)
{
c = null;
}
....
g(ref c);
Related
This question already has answers here:
Accessing object property as string and setting its value
(11 answers)
Closed 11 months ago.
Is it possible to make a dictionary that points to getters & setters?
For example:
public class test
{
public Dictionary<string, int> pointer = new Dictionary<string,int>()
{
{ "a", a }
}; // the goal is to do pointer["a"] = someInt or print(pointer[a])
public int a { get { return b;} set { b = 2; } }
public int b;
}
Odds are you are not actually trying to put getters and setters in a dictionary, and that you are trying to access properties by their names. This is easily possible through Reflection. If that is what your question is about you should mark this as duplicate and close the question.
But, the answer to your actual question is yes. It is possible to make a dictionary that points to getters and setters. You will need to use some lambda programming. Here is one way to do it:
public class test
{
public Dictionary<string, GetSet<int>> pointer;
public test()
{
pointer = new Dictionary<string, GetSet<int>>()
{
{ "a", new(()=>a,i=>a = i) }
};
}
public int a { get { return b; } set { b = 2; } }
public int b;
}
public class GetSet<T>
{
private readonly Func<T> _get;
private readonly Action<T> _set;
public GetSet(Func<T> get, Action<T> set)
{
_get = get;
_set = set;
}
public T Get() => _get();
public void Set(T value) => _set(value);
}
Usage:
var test = new test();
var property = test.pointer["a"];
var value = property.Get();//returns the value of test.a
property.Set(3);//sets test.a to 3
This is the Question I am trying to solve in C Sharp.
I am getting an error:
Error Expected ; or = (cannot specify constructor arguments in declaration)
Can anyone help me to solve this or guide me to solve this?
namespace program
{
public class Integer
{
private int intvar;
public Integer()
{
intvar = 0;
}
public Integer(int x)
{
intvar = x;
}
public void display()
{
Console.Write(intvar);
Console.Write("\n");
}
public void add(Integer x, Integer y)
{
intvar = x.intvar + y.intvar;
}
}
class Program
{
static void Main(string[] args)
{
Integer a(5),b(45);
Integer c;
c.add(a,b);
c.display();
Console.ReadLine();
}
}
}
You cannot create objects like that in C#. Im assuming you come from C++ where this syntax is possible.
In C# you have to create objects using new:
Integer foo = new Integer(45);
To create a new instance of a type, you have to invoke one of the constructors of that type using the new operator. For example:
class Program
{
static void Main(string[] args)
{
var a = new Integer(5);
var b = new Integer(45);
var c = new Integer(); //result instance
c.add(a, b);
c.display();
Console.ReadLine();
}
}
This question already has answers here:
A field initializer cannot reference the nonstatic field, method, or property - while creating a list
(2 answers)
Error 1 A field initializer cannot reference the non-static field, method, or property
(2 answers)
Closed 4 years ago.
In java I can do this
public class HelloWorld
{
public static void main(String[] args)
{
OtherClass oc = new OtherClass();
oc.a.run();
}
}
public class OtherClass
{
public int s = 3;
public Runnable a = () -> System.out.println("s is " + s);
}
The output will be s is 3. When I try this in C# with this code
using System;
namespace SomeNamespace
{
public class Program
{
public static void Main(string[] args)
{
MyClass m = new MyClass();
m.a.Invoke();
}
}
public class MyClass
{
public int s = 3;
public Action a = () => Console.WriteLine(s);
}
}
Then I get (23:51) A field initializer cannot reference the non-static field, method, or property 'SomeNamespace.MyClass.s'
This question already has answers here:
How to Access a static method in c#?
(5 answers)
Closed 6 years ago.
I have a public class and it contains a static function as given below:-
public class demo1
{
public static int GetFactorials(int number)
{
int result = 1;
for (int i = 1; i <= number; i++)
{
result *= i;
}
HttpContext.Current.Response.Write(result);
return result;
}
}
How to call this static function GetFactorials?
Static function/method doesn't requires object.
demo1.GetFactorials(10);
It is simple:
demo1.GetFactorials(123);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C# optional parameters on overridden methods
using System;
namespace Apple
{
class A
{
public virtual void Func(int a=4){
Console.WriteLine(" A Class: "+a);
}
}
class B : A
{
public override void Func(int a = 12)
{
Console.WriteLine(" B Class: "+ a);
}
}
public class Program
{
static void Main(string[] args)
{
A ob = new B();
ob.Func();
Console.ReadLine();
}
}
}
// Output: B Class: 4
Default parameters are filled at compile time, and the code references a variable ob through the base class. The virtual method override works at run time, as expected. You could achieve the desired effect by using method overload:
class A
{
public void Func(int value)
{
}
public virtual void Func()
{
Func(4);
}
}
class B: A
{
public override void Func()
{
Func(12);
}
}
The compiler places the the default parameter value based on the type of the object and is done during the compile time.
Hence the compiled code would look like:
using System;
namespace Apple
{
public class Program
{
private static void Main(string[] args)
{
A ob = new B();
ob.Func(4);
Console.ReadLine();
}
}
}
You could get the desired result by doing this:
public class Program
{
static void Main(string[] args)
{
A ob = new B();
((B)ob).Func();
Console.ReadLine();
}
}
Because you creating instance of Class A which is referring to the address of class B.
A ob = new B();
Since the instance is of class A, the method you calling is pointing to method in class A.
You can check this by putting debug and then execute the code.
instead if you create instance of class B ie
B ob = new B();
it will call the method Fun() from class B and will display output as
" B Class: 12"
the default parameter value is Static binding.