Title of my question is quite different from my question. I am sorry for that because i don't know what should be title for this question.
Suppose i have one class let's say "ClassA"
Class ClassA
{
int x=5;
int y=6;
int z=7;
static public int sum(int a,int b)
{
return (a+b);
}
}
now i need some logic by which i can use sum() like following in another class
int c = ClassA.sum(x,y);
int d = ClassA.sum(x,z);
here i don't need to declare "x","y" and "z" variable. It must be a consider a value that is defined in ClassA.
My question can be silly but just help me.
What should i do ????
You can declare x, y, and z as const:
class ClassA
{
public const int x=5;
public const int y=6;
public const int z=7;
static public int sum(int a,int b)
{
return (a+b);
}
}
Then do:
int c = ClassA.sum(ClassA.x,ClassA.y);
You can pass an instance of an object to a static method, it can invoke instance members on that object.
class ClassA
{
int x=5;
int y=6;
int z=7;
static public int sum(ClassA obj)
{
return (obj.x+ obj.y);
}
}
In other class create instance of class ClassA and pass this object to static method
ClassA objA = new ClassA();
int c = ClassA.sum(objA);
Check if the following code fits your requirement.
Create an enum as:
public enum MyEnum
{
x,y,z
}
The classA code will be:
class ClassA
{
static int x = 5;
static int y = 6;
static int z = 7;
static public int sum(MyEnum enum1, MyEnum enum2)
{
int a = 0;
int b = 0;
switch (enum1)
{
case MyEnum.x:
a = x;
break;
case MyEnum.y:
a = y;
break;
case MyEnum.z:
a = z;
break;
default:
break;
}
switch (enum2)
{
case MyEnum.x:
b = x;
break;
case MyEnum.y:
b = y;
break;
case MyEnum.z:
b = z;
break;
default:
break;
}
return (a + b);
}
}
I have set x,y,z as static because your method is static and you cannot access instance members in static method. Or you can replace static with const while declaring variable x,y,z.
Hope this helps.
Related
The following code initializes the base class from derived class using 'base' keyword by calling the base class constructor from the derived class.
class A
{
public int a;
public int b;
public A(int x, int y)
{
a = x;
b = y;
}
}
class B : A
{
int c;
public B(int s, int n,int z)
: base(s, n)
{
c = z;
}
public int add()
{
return a + b+c;
}
}
class Program
{
static void Main(string[] args)
{
B b = new B(2, 3,5);
Console.WriteLine(b.add());//the output is 10 OK
}
}
Question
what happens if a derived class inherits from more than one base class.then how to initialize all the base classes from the derived class using base keyword(how to call the base class constructors).**
class A
{
public int a;
public int b;
public A(int x, int y)
{
a = x;
b = y;
}
}
class B:A
{
public int d;
public int e;
public B(int x, int y)
{
d = x;
e = y;
}
}
class C:B
{
}
then from class C how to initialize both the base classes using base keyword.
A class can only inherit from one base class.
If your base class inherits from some other base class, your base class will initialize its base class, just like any other class.
As others have said, in .Net you can only inherit from one Class, however you can implement a large number of interfaces. The difference and how to achieve an effect similar to inheriting from a number of classes is out of scope of this question.
In your example (using the previous code sample as a standard) you would have to do the following
class A
{
public int a;
public int b;
public A(int x, int y)
{
a = x;
b = y;
}
}
class B:A
{
public int d;
public int e;
public B(int x, int y): base (x, y)
{
d = x;
e = y;
}
}
class C:B
{
public C(int m, int n) : base(m, n)
}
This would in effect instantiate C, which then calls the constructor of B which then calls the constructor of A.
Inheriting from multiple classes is not allowed in .Net. (Cause that would lead to Diamond problem). But implementing from multiple interface and multi level inheritance is allowed.
In first snippet first A's constructor is called followed by B's constructor.
In second snippet first A's constructor will be called, then B's followed by C's constructor.
You can try this out like so in LINQPad:
Copy-pastable code below (note, will only be 'valid' in LinqPad, due to } in odd places - requirement of linqpad)
var test = new C(1, 2);
}
class A
{
public int a;
public int b;
public A(int x, int y)
{
a = x;
b = y;
Console.WriteLine("A");
}
}
class B : A
{
public int d;
public int e;
public B(int x, int y) : base(x, y)
{
d = x;
e = y;
Console.WriteLine("B");
}
}
class C : B
{
public C(int x, int y) : base(x, y)
{
Console.WriteLine("C");
}
I tried this:
class Program
{
public delegate int add(int x, int y);
public class ff
{
public static int addNumbers(int x, int y)
{
return x + y;
}
public static int substractNumbers(int x, int y)
{
return x - y;
}
static void Main(string[] args)
{
Delegate delegare = new add(ff.addNumbers);
Console.WriteLine(delegare(3,4));
}
}
I don't see why I'm getting this error"Method name expected".
When I use a delegate with a void function it works.
Can someone help me?
The type of your delegare variable is just Delegate. That could refer to any delegate. In order to invoke a delegate (in the normal way), you should have an expression of the appropriate type.
After fixing the naming conventions and removing the unnecessary nested class - and demonstrating a method group conversion - your code looks like this:
using System;
public delegate int Int32Operation(int x, int y);
class Program
{
public static int AddNumbers(int x, int y)
{
return x + y;
}
public static int SubtractNumbers(int x, int y)
{
return x - y;
}
static void Main(string[] args)
{
Int32Operation op = new Int32Operation(AddNumbers);
Console.WriteLine(op(3, 4)); // Prints 7
op = SubtractNumbers; // Method group conversion
Console.WriteLine(op(3, 4)); // Prints -1
}
}
You should try this out:
add delegare = new add(ff.addNumbers);
The type of your delegate should be add since you defined so.
I have two interfaces A,B both has same method declarations. I have a class C inheriting from interfaces A,B. I have another class D inheriting from C. Now i want to access the implemented methods in C from D
interface A
{
int add(int x, int y);
int mul(int x, int y);
}
interface B
{
int add(int x, int y);
int mul(int x, int y);
}
public class C : A,B
{
int A.add(int x,int y)
{
return x + y;
}
int A.mul(int x,int y)
{
return 0;
}
int B.add(int x, int y)
{
return x;
}
int B.mul(int x, int y)
{
return y;
}
}
class D : C
{
}
How to access the methods in C from D?
How to access the methods in C from D?
You have to use a reference with a compile-time of the relevant interface. For example:
class D
{
public void FooA()
{
A a = this;
Console.WriteLine(a.mul(...));
}
public void FooB()
{
B b = this;
Console.WriteLine(b.mul(...));
}
}
Of course you don't need the local variable - you can cast:
Console.WriteLine(((A) this).mul(...));
... but it gets a bit ugly.
This is just because you're using explicit interface implementation. If you implemented one of the interfaces implicitly, you could just call the methods directly as normal... but explicit interface implementation only allows a member to be called via that interface.
An explicit interface method invocation should always work
((A)this).mul(1,1);
You can use this code because you have to specify the interface from which you want to use the method (A or B):
((A)this).add(1, 1);
As others already suggested casting is of course one way of doing this. It's quick and simple but if you're going to use it a lot it's annoying. The way out in this case are properties that give access to the members provided by the interface and that conveniently group them thus simplifying their usage:
Easy access without additional casting (you do it only once inside the property getters - see below the C-class):
class Program
{
static void Main(string[] args)
{
C c = new C();
c.As.add(1, 2);
}
}
Interfaces:
public interface A
{
int add(int x, int y);
int mul(int x, int y);
}
public interface B
{
int add(int x, int y);
int mul(int x, int y);
}
C-class:
public class C : A, B
{
// Methods from the A-interface.
public A As { get { return (A)this; } }
// Methods from the B-interface.
public B Bs { get { return (B)this; } }
int A.add(int x, int y)
{
return x + y;
}
int A.mul(int x, int y)
{
return 0;
}
int B.add(int x, int y)
{
return x;
}
int B.mul(int x, int y)
{
return y;
}
}
D-class:
public class D : C
{
public D()
{
base.As.add(1, 2);
base.Bs.add(3, 4);
}
}
Can you check this,
using System;
public class Program
{
public static void Main()
{
D ds=new D(10,12);
int valueAddtion=((A)ds).add(20,122);
int valueMultiplication=((B)ds).mul(20,11);
Console.WriteLine("Mainapplicatin Value of A= " +valueAddtion+" multiplication value= "+valueMultiplication);
}
}
// your code segment here
class D : C
{
public D()
{
int valueAdd=((A)this).add(10,11);
int valueMul=((B)this).mul(20,11);
Console.WriteLine("Addition Value of A= " +valueAdd+" multiplication value= "+valueMul);
}
public D(int x,int y):this()
{
int valueAdd=((A)this).add(x,y);
int valueMul=((B)this).mul(x,y);
Console.WriteLine("Paremeterized Value of A= " +valueAdd+" multiplication value= "+valueMul);
}
}
Output will be,
Addition Value of A= 21 multiplication value= 11
Paremeterized Value of A= 22 multiplication value= 12
Mainapplicatin Value of A= 142 multiplication value= 11
Please could someone provide me with a simple extension method that for example squares a number.
I have drawn up so pseudo code:
class Program
int = x
---------------------
public static int square (this int x)
return x * square
public static class NumberExtensions
{
public static int Square(this int n)
{
return n*n;
}
}
Now you can say:
int number=5.Square();
Here is how you would write the method:
public static class ExtnMethods
{
public static int Square(this int x)
{
return x * x;
}
}
Some important things to note about the above code:
The class must be static and non-abstract
The parameter this int x specifies that the method acts on an int
You would use it like so:
Console.WriteLine(5.Square());
// prints 25
public static class SomeClass {
public static int Square(this int x) {
return x * x;
}
}
The extension method:
static class MathExtensions {
public static Int32 Square(this Int32 x) {
return x*x;
}
}
How to use it:
var x = 5;
var xSquared = x.Square();
In this example I tried to show you how use multiple Extension method in a single expression.
class Program
{
static void Main(string[] args)
{
int x = 13;
var ans = x.Cube().Half().Square();
Console.WriteLine(ans);
}
}
static class IntExtensions
{
public static int Half(this int source)
{
return source / 2;
}
public static int Cube(this int source)
{
return (int)Math.Pow(source, 3);
}
public static int Square(this int source)
{
return (int)Math.Pow(source, 2);
}
}
Why is it necessary to make a function STATIC while using delegates in C# ?
class Program
{
delegate int Fun (int a, int b);
static void Main(string[] args)
{
Fun F1 = new Fun(Add);
int Res= F1(2,3);
Console.WriteLine(Res);
}
**static public int Add(int a, int b)**
{
int result;
result = a + b;
return result;
}
}
It's not "necessary". But your Main method is static, so it can't call a non-static method. Try something like this (this isn't really a good way to do things—you really should create a new class, but it doesn't change your sample much):
class Program
{
delegate int Fun (int a, int b);
void Execute()
{
Fun F1 = new Fun(Add);
int Res= F1(2,3);
Console.WriteLine(Res);
}
static void Main(string[] args)
{
var program = new Program();
program.Execute();
}
int Add(int a, int b)
{
int result;
result = a + b;
return result;
}
}
Your function needs to be static because you're calling from a static method, Main. You can make the method non-static:
class Program
{
delegate int Fun (int a, int b);
static void Main(string[] args)
{
Program p = new Program(); // create instance of Program
Fun F1 = new Fun(p.Add); // now your non-static method can be referenced
int Res= F1(2,3);
Console.WriteLine(Res);
}
public int Add(int a, int b)
{
int result;
result = a + b;
return result;
}
}
In this case, because you aren't creating an instance of any class, the only alternative is a static function. Were you to instantiate an object of type Program, then you could use an instance method instead.
Delegates basically follow the same rules as methods. In the example provided your delegate must be static because you are calling it from a static method. In the same vein this will not work:
static void Main(string[] args)
{
int Res = Add(3, 4);
Console.WriteLine(Res);
}
public int Add(int a, int b)
{
int result;
result = a + b;
return result;
}
However if you moved things into a non static context like this:
class MyClass
{
public MyClass()
{
Fun F1 = new Fun(Add);
int Res = F1(2, 3);
Console.WriteLine(Res);
}
public int Add(int a, int b)
{
int result;
result = a + b;
return result;
}
}
You can have a delegate with a non-static method.
No need to create a static method to pass in delegate.
But the non static method should be declared in different class and have to be accessed with instance of that class.
DelegateName DN = new DelegateName ( instance of the class . Method Name)