Member pass-through in C#? - c#

Go has a nice feature where members of a nested struct are automatically accessible via its parent struct:
// Server exposes all the methods that Logger has
type Server struct {
Host string
Port int
*log.Logger
}
// initialize the embedded type the usual way
server := &Server{"localhost", 80, log.New(...)}
// methods implemented on the embedded struct are passed through
server.Log(...) // calls server.Logger.Log(...)
// the field name of the embedded type is its type name (in this case Logger)
var logger *log.Logger = server.Logger
Is the same possible in C#, for example using implicit casts?
struct B
{
public int x;
}
struct A
{
public B b;
}
var a = new A();
a.b.x = 1; // How it usually works
a.x = 1; // How Go works, assuming x is unambiguous

There is no such concept in C#. You can add such properties by yourself, but it will be very confusing for other developers who see your code.
struct B
{
public int x;
}
struct A
{
public B b;
public int x {
get { return b.x; }
set { b.x = value; }
}
}
}
var a = new A();
a.b.x = 1;
a.x = 1;
However if you switch to classes instead of structs - you can have similar behavior using inheritance:
class B
{
public int x;
}
class A : B
{
}
var a = new A();
a.x = 1;

An embedded Golang struct can be seen as a sort of "inheritance". If you want simulate this behaviour in C# you should use a class instead of struct (here if you want know the difference between struct and class).
Something like this:
public class A {
public int X {get; set;}
}
public class B : A{
B : base() {}
}
...
var a = new B();
a.X = 24;

Related

C# - How can I access the value using properties of classes in an override method?

My Code:
public class A
{
public virtual void displayDetailInfo()
{
}
}
public class B : A
{
public String _a;
public int _n;
public B() { }
public B(String _a, int _n)
{
this._a = _a;
this._n = _n;
}
public String A
{
get { return _a; }
set { this._a = value; }
}
public int N
{
get { return _n; }
set { this._n = value; }
}
public override void displayDetailInfo()
{
Console.WriteLine(A);//To obtain value entered in Main(i.e. f.A)
Console.WriteLine(N);//To obtain value entered in Main(i.e. f.N)
}
}
public class Program
{
public static void Main(String[] args)
{
A v = new A();
A v1 = new B();
B f = new B();
f.A = Console.ReadLine(); //Value to be accessed
f.N = Convert.ToInt32(Console.ReadLine()); //Value to be accessed
v1.displayDetailInfo();
}
}
How can I get the value(f.A and f.N) I entered in Main accessed from the overrided method in class B(i.e. displayDetailInfo()). The code I wrote doesn't obtains any value(i.e. Console.WriteLine(A) gives no value of f.A). So how can I get the value of f.A and f.N from overrided displayDetailInfo()?
Whenever you use new to create a new object, you are creating a new, independent object that has its own state.
Here, you are creating 3 separate objects - v, v1, f.
A v = new Vehicle();
A v1 = new B();
B f = new B();
Changing a property of one of these objects does not affect the properties of the other two objects whatsoever.
Here you change the properties of f, but the properties of v1 is not affected.
f.A = Console.ReadLine();
f.N = Convert.ToInt32(Console.ReadLine());
This is why when you call v1.displayDetailInfo(), it prints null and 0. null and 0 are the default values of string and int respectively. v1's properties have not been set yet, so they hold the default values.
To fix this, just call f.displayDetailInfo() instead.
You can't do this because you v1 is a different instance of B than the one you want to get the values from (f)
Calling f.displayDetailInfo() should give you the result you want
You are setting A of a different object than what you are calling displayDetailInfo on.
I think you meant to do this:
public static void Main(String[] args)
{
A v = new Vehicle();
B v1 = new B();
v1.A = Console.ReadLine(); //Value to be accessed
v1.N = Convert.ToInt32(Console.ReadLine()); //Value to be accessed
v1.displayDetailInfo();
}
It's not possible for v1 to have data you entered in f. The class is just a blueprint, v1 and f are different instances that exist in different parts of the heap.
v1.A is not the same as f.A and v1.N is not the same as f.N
To see the values you entered, you better call:
f.displayDetailInfo()
Also, you're using properties wrong. If you want to use backing fields, the ones with the underscores (_n and _a), you better make them private. And unless you want to have additional logic to the getters or setters you are better off not using backing fields altogether and use auto implemented properties:
public string A { get; set; }
public string N { get; set; }

Inheritance same methods implementation

I have the following code. (in c#)
interface 1:
public interface iBclass
{
int addition(int a);
int s(); //and more methods from this ......
}
interface 2:
public interface iAclass
{
int addition(int a);
//more methods.....
}
Class that inherits both interfaces:
public class dClass : iAclass , iBclass
{
int iAclass.addition(int a)
{
return 0;
}
int iBclass.addition(int a)
{
return 1;
}
public int s()
{
return 3;
}
}
the problem is i am not able to access the Method iAclass.addition(int a) and iBclass.addition(int a) with the d object.
dClass d = new dClass();
how can i access those method by 'd' object? and why those interface methods are not allow to define as public?
The interfaces are implemented explicitly. So you can only call them by using the interface:
dClass d = new dClass();
iAclass a = (iAclass)d;
a.addition(123); // Calls implementation for iAclass
iBclass b = (iBclass)d;
b.addition(123); // Calls implementation for iBclass
See this link for details.

C#- static variables from class to class

First of all, take a look at my below code:
class A
{
public static int Flag()
{
return 0;// set initial value=0
}
B b= new B();
public void afunc()
{
b.bfunc();
}
}
And class B recieves and sends static variable:
class B
{
A a= new A();
int flag= a.Flag();
public void bfunc()
{
if(flag==0)
{
flag=1;//???? is this wrong???
//do some thing
}
}
}
Class A send to B a static variable with initial value=0; then class A call bfunc from class B. In bfunc() I set flag=1. I'm a new to C#. Can you share me how class A recieves back flag=1 sended by class B. I mean which syntax?
a few things are wrong here
Flag is a method on A, so you cannot change its "value"
Flag is static therefore it does not have an instance which is what I think you want
I suspect you want Flag to be a property of A
public int Flag{get;set;}
You are making new instances of A and B, which may be correct for you but be weary this means you are not referencing existing instances
You have two options
A
this.Flag = b.bFunc();
public int bFunc()
.... return 1;
B
public void bFunc()
... a.Flag = 1;
If you really want static variable then
public static int Flag = 0;
A.Flag = x
Were is no static variable here, you only have a static function int Flag(). To get value of a flag in class A, you must return this value from function bfunc() like this:
public int bfunc()
{
if(flag==0)
{
flag=1;
return flag;
}
}
I don't know if I understood you properly because there are many things wrong with your code. Flag should be a property instead of a method so you can store your value. The way you used it was just tossing out a zero.
First, your two classes. Keep in mind that usually properties should be used as accesssors to private fields, but let's do it the simplest way.
class A
{
public static int Flag = 0;
}
class B
{
public void bfunc()
{
if (A.Flag == 0)
{
A.Flag = 1;
}
}
}
Then use them as follows to change Flag's value.
B bObject = new B();
bObject.bfunc();
// A.Flag is now 1.
Note that bfunc() will change Flag's value to 1 only if it was 0 before.

C# Inheritance : modify base class variable from derived class

I have a base class that looks as follows
public class base
{
public int x;
public void adjust()
{
t = x*5;
}
}
and a class deriving from it. Can I set x's value in the derived class's constructor and expect the adjust() function to use that value?
Yes, that should work entirely as expected, even though your code sample does not quite make sense (what is t?). Let me provide a different example
class Base
{
public int x = 3;
public int GetValue() { return x * 5; }
}
class Derived : Base
{
public Derived()
{
x = 4;
}
}
If we use Base:
var b = new Base();
Console.WriteLine(b.GetValue()); // prints 15
...and if we use Derived:
var d = new Derived();
Console.WriteLine(d.GetValue()); // prints 20
One thing to note though is that if x is used in the Base constructor, setting it in the Derived constructor will have no effect:
class Base
{
public int x = 3;
private int xAndFour;
public Base()
{
xAndFour = x + 4;
}
public int GetValue() { return xAndFour; }
}
class Derived : Base
{
public Derived()
{
x = 4;
}
}
In the above code sample, GetValue will return 7 for both Base and Derived.
Yes, it should work.
The following, slightly modified code will print 'Please, tell me the answer to life, the universe and everything!' 'Yeah, why not. Here you go: 42'
public class Derived : Base
{
public Derived()
{
x = 7;
}
}
public class Base
{
public int x;
public int t;
public void adjust()
{
t = x * 6;
}
}
class Program
{
static void Main(string[] args)
{
Base a = new Derived();
a.adjust();
Console.WriteLine(string.Format("'Please, tell me the answer to life, the universe and everything!' 'Yeah, why not. Here you go: {0}", a.t));
}
}

Calling base class property from object of derived class without marking it virtual

How to call x of class a from object of class b without marking x of class a as virtual.
Is it possible
public class a { public int x { get; set; } }
public class b : a { public int x { get; set; } }
public class c {
a _a = new a();
b _b = new b();
public c()
{
int y=_a.x;
y=_b.x;
_b.x = y;
}
}
base.x() should work inside the b type (but that isn't what you have here).
In the "method hiding" scenario (what you have), it also largely depends on what a variable is typed as, so casting to a should work:
a tmp = _b;
tmp.x = ... // talks to a.x, not b.x
or more succinctly:
((a)_b).x = ... // talks to a.x, not b.x
Use typecast:
public class c
{
a _a = new a();
b _b = new b();
void Test() {
int y = _b.x; // This is "x" of "b"
a _b_as_a = (a)_b;
int z = _b_as_a.x; // This is "x" of "a" of "b"
}
}
Besides typecasting (as above, although that won't work if you don't know its parent's type) you can't, so I would advise re-structuring one of those classes. You should probably make x virtual, or change the property x inside class b to something different, or call base.x inside b's x implementation.

Categories