How to hide static method - c#

Let's say I have a classes, like that:
class A
{
public static int Count()
}
class B : A
{
}
class C : A
{
}
How can I hide this static method for class B but not for C?

You can't, basically. Heck, if it's public then anyone can call it.
You could make it protected which would allow it to be called from within B or C but not elsewhere... but you still couldn't differentiate between B and C.

You could do it by creating another class, let's call it Special, that inherits A. Then you would make C inherit from Special and B inherit from A. Also, you would have the static method protected, that means only classes that inherited Special will have access to it.
class A
{
}
class Special : A
{
protected static int Count()
}
class B : A
{
}
class C : Special
{
}

The only solution would be to change your class hierarchy. It's not worth the hassle and WTF moments you will get in code reviews it if you ask me.
class ABase
{
}
class A
{
public static int Count()
}
class B : ABase
{
}
class C : ABase
{
}

Related

How to set variable values in parent class only for specific child class in C#?

I have 3 classes A, B and C.
class A
{
protected string name="demo";
}
class B: A
{}
class C: A
{}
I want to set the value of name as "demo1" in parent class A only for the child class C and not for B.
How can I achieve this?
You can add a constructor to A that allows you to set the value:
class A
{
private string name="demo";
public A() {}
public A(string name)
{
this.name = name;
}
}
class B: A
{}
class C: A
{
public C() : base("demo1")
{
}
}
Because in your code, A has a private field, no other class can see or modify it.
If you make the field protected, this means it is visible to itself and to classes that derive from it, but not visible from outside the classes.
class A
{
protected string name="demo";
}
class B: A
{}
class C: A
{
C()
{
name = "ClassC";
}
}
Could you have them all implement that same interface and have class C inherit from class A but class B only implements the interface. It's all a little vague so hard to tell the best way.
Using inheritance, the simplest solution would put the specified variable in another class that inherits from A and will be only inherited by C (while B still directly inherits from A).
class A
{}
class B: D
{}
class C: A
{}
class D: A
{
protected string name="demo";
}
class A
{
protected virtual string name="demo";
}
class B: A
{}
class C: A
{
protected override string name="demo1";
}

Extending multiple/different objects based on required functionality?

Is there a design pattern that allows me to define an object as extending another object depending on the functionality that I need? For example if I have the following classes:
using System;
namespace C_
{
public class Program
{
static void Main(string[] args)
{
Console.WriteLine("------------------------------------------------");
C c = new C();
Console.WriteLine("------------------------------------------------");
D d = new D();
Console.WriteLine("------------------------------------------------");
}
}
public abstract class A
{
public A() {}
public void DoSomething()
{
Console.WriteLine("Doing something");
}
public virtual void CallSomething()
{
DoSomething();
}
}
public abstract class B : A
{
public B() {}
public override void CallSomething()
{
Console.WriteLine("Do something extra");
DoSomething();
}
}
public class C : A
{
public C()
{
CallSomething();
}
}
public class D : B
{
public D()
{
CallSomething();
}
}
}
Essentially class B is an extension of class A. The minimum functionality I want is defined in class A while in some cases I may need more which is defined in class B. Is it possible to have another class extend either A or B depending on what functionality is required at runtime? I could create separate classes each extending A or B but that would lead to a lot of repeated code
EDIT: Added a more concrete example. Classes C and D have the same functionality except that D requires a little more done before it can proceed hence extending B instead of A. Is there a design pattern where I wouldn't need class D and can pick and choose the functionality of C based on what I need?

is this possible in c# ? only one class access base class method and second class can not access the same base class method

A is base class
B is derived from A and also C is derived from A
I want only B can access the method of A , C an not access of that same method of A.
class A {
protected void Foo() {
}
}
class B : A {
void Bar() {
this.Foo(); // OK
}
}
class C : A {
void Baz() {
this.Foo(); // I don't want to permit this
}
}
HOW IT POSSIBLE IN c#
I think this look like a problem for Interface segregation principle:
Clients should not be forced to depend upon interfaces that they don't
use.
But in your case this can be rephrased for the class inheritance.
Create pure base class (without a method you want to hide from class C)
public class Base
{
protected void SomeDummyMethod()
{
}
}
Then create your A class which inherit from Base and add a method you what to share for class B
public class A : Base
{
protected void YourFooMethod()
{
}
}
Create B class which inherit from A and will have access to all functionality including YourFooMethod
public class B : A
{
public void Bar()
{
this.YourFooMethod();
}
}
And finally your C class which have all base functionality except YourFooMethod method
public class C : Base
{
public void Bar()
{
this.YourFooMethod(); //Compile error: YourFooMethod is not a member of...
}
}
I suppose you could write code in class A that checks the calling class name against a white list or a black list and throws an exception in the cases you want to disallow, but I would not recommend doing this. That would be very difficult to maintain, and class A should not need to know about every class that extends it.
What you are trying to do is really honestly a bad idea.
C# (and .NET in general) has the access modifiers:
public - Anyone can access
private - Only the containing scope/type can access
protected - Only the containing type and its derived types can access
internal - Only types defined in the same Assembly (or InternalsVisibleTo Assemblies) can access
protected internal - The set-union of protected and internal can access.
You're asking for something in-between private and protected, where you can manually restrict access to named types.
This is not currently possible to enforce, at least at compile-time, in .NET - though if types A and B exist in the same assembly and C exists in a different assembly then internal would work.
At runtime you could enforce this with code-access-security, or more simply: using reflection to get the calling-class's name (this.GetType()), or use a password:
or more simpler: a password requirement:
class A {
private Boolean isAllowedAccess;
protected A(String password) {
this.isAllowedAccess = password == "12345abc";
}
protected void Foo() {
if( !this.isAllowedAccess ) throw new InvalidOperationException();
}
}
class B : A {
public B() : base("12345abc") {
}
void Bar() {
this.Foo(); // OK
}
}
class C : A {
public C() : base(null) {
}
void Baz() {
this.Foo(); // I don't want to permit this
}
}

How to force single inheritance?

I have two classes:
public class A
{
}
public sealed class B : A
{
}
And I want that only my B class (defined in the same assembly of A) can inherits A.
How can I do?
And I want that only my B class (defined in the same assembly of A) can inherits A. How can I do?
There is simply no need for inheritance then..
public sealed class B
{
//includes all methods of A and B
}
You can use a internal constructor, this prevents other assemblies from using your class.
public class A
{
internal A() { }
}
public sealed class B : A { }
And, if you don't care about performance:
public class A
{
public A()
{
Type c = GetType();
if (c != typeof(A) && c != typeof(B)) throw ....;
}
}
I suggest you use composition instead of inheritance to restrict the visibility of base type members.
If only B can inherit A, then do you really need inheritance in the first place?
If it's to split a class that became too big, you're not following good practice as it is not likely to have a single responsibility.
Now if for some awkward reason you actually do need this, you can try that:
public class B
{
private sealed class A : B
{
}
}
And tweak it to your needs.

c# get method form inherited class

we have the classes:
class A
{
}
class B:A
{
public void metod(){}
}
and
static class C
{
static void met(B clas)
{
A test = clas as B;
// is it any way to get method "metod " from B via test?
}
}
I know that this look like nonsense but the idea is right, I cannot make B object in C class.
Child can access parent through base. For e.g.
base.SomeMethod();
because when inheriting child knows who parent is and that parent class becomes base
but there is no keyword like child because base does not know which class will inherit it.
A child can have a single parent but a parent can have one or more child.
You need to convert it back like this and than you can access
Base derivedInstance = new Derived();
Derived child= (Derived)derivedInstance;
child.Callmethod();
in short
A test = clas as B;
((B)test).method();
You can cast test to B, or simply use the variable declared as B in the first place:
((B)test).metod();
or
clas.metod();
class A
{
public virtual void method(){}
}
class B:A
{
public override void method(){}
}
and
static class C
{
static void met(B clas)
{
A test = clas as B;
test.method(); //this may be what you want?
}
}

Categories