This question already has answers here:
In C#, what is the difference between public, private, protected, and having no access modifier?
(19 answers)
Closed 6 years ago.
Suppose I have a base class:
public class A {
public float someValue;
<Access Modifier Here> float SomeValue {
get {
return someValue;
}
}
}
And I want to derive from it:
public class B : A {
public float SomeProperty {
get {
return SomeValue;
}
}
}
What access modifier would I use if I want to make the SomeValue property only available to the deriving class and not anywhere else?
for only derived classes.. use protected
Protected means that access is limited to the containing class or types derived from the containing class.
Related
This question already has answers here:
Inconsistent accessibility with protected internal member
(1 answer)
Inconsistent Accessibility: Parameter type is less accessible than method
(13 answers)
What is the difference between 'protected' and 'protected internal'?
(12 answers)
What are the default access modifiers in C#?
(10 answers)
Is there an easy way to use InternalsVisibleToAttribute?
(4 answers)
Closed 2 years ago.
I have the following code in C# (.Net)
public class A
{
protected internal enum Color {WHITE, BLACK};
}
public class B
{
protected internal int methodOne(A.Color color)
{
if (color == A.Color.WHITE)
return 1;
return 2;
}
}
But I'm getting the following error: error CS0051: Inconsistent accessibility: parameter type 'A.Color' is less accessible than method 'B.methodOne(A.Color)'
Why does it say the parameter is "less accessible" when both the parameter type and the method have the same access modifier (protected internal)?
Update #1: this is what I really want to achieve:
public class A
{
internal enum Color {WHITE, BLACK};
}
public class B
{
protected int methodOne(A.Color color)
{
if (color == A.Color.WHITE)
return 1;
return 2;
}
}
public class C : B
{
int methodTwo()
{
return methodOne(A.Color.WHITE);
}
}
I want to keep enum Color as internal, and I want methodOne to be accessible only by derived classes. Is this possible?
This question already has answers here:
C#: override a property of the parent class
(3 answers)
Closed 4 years ago.
I have a class Base which is like:-
class Base
{
public virtual string a=>SomeMethod(a);
}
I need to override the variable a in Base.
class Derived: Base
{
public string a ="Hello";
}
Is this correct?What is the correct way to inherit from Base and override the value of a?
Thanks!
Use the keyword 'override'. However, you cannot use it on fields. If you want to override the virtual property in your base class, create a corressponding property in your derived class.
class Derived: Base
{
public override string a => "Hello";
}
This question already has answers here:
Can I override a property in c#? How?
(5 answers)
Closed 6 years ago.
in base LocalStorage
public class BaseStorageRepository<T>
{
protected string OneKey = null;
protected string ListKey = null;
public async Task UpdateAllAsync(List<T> data)
{
await BlobCache.LocalMachine.InsertObject(ListKey, data);
}
}
in child
public class CompanyStorageRepository : BaseStorageRepository<Company>
{
protected new string OneKey = "Company";
protected new string ListKey = "CompaniesList";
}
When execution in
UpdateAllAsync
then OneKey == null;
But why ?
After all, I redefined the property in the derived class
After all, I redefined the property in the derived class
No, you hid the field (not property) in the derived class. Effectively the derived class now has two distinct OneKey fields. The base class code is still looking at the field from the base class, not the new one in the derived class.
Also note that fields can't be virtual or overridden. You could set the field value in the derived class's constructor, though:
public class CompanyStorageRepository : BaseStorageRepository<Company>
{
public CompanyStorageRepository()
{
OneKey = "Company";
ListKey = "CompaniesList";
}
}
A marginally better design would be to use properties instead of fields. With properties, you can control (and detect) when a value is changed, and you can change how the value is stored without breaking the classes semi-public signature.
This question already has answers here:
method without access modifier
(8 answers)
Default Class Accessibility in C#
(4 answers)
Closed 8 years ago.
If we do not specify Public/Private/Protected, what will it be?
Is there something known as a private class?
1: that depends on whether the class is nested or not. A top level class defaults to internal. A nested class defaults to private.
class TopLevelClass {
class PrivateClass {
}
}
2: yes, but only for nested classes:
class TopLevelClass {
private class ExplicitlyPrivateClass {
}
class ImplicitlyPrivateClass {
}
}
If you don't specify Public/Private/Protected for a main class it will be internal, for a nested class the default access specifier will be private.
private class exists. You can access a private class only if it is declared inside another class. Means it is private to the parent class as
class example //which is default interal
{
private class ex
{
}
class ex1 //default private
{
}
}
1) If no modifier is specified, the visibility will depend on the situation where it is omitted; the topic is discussed in this question.
2) In the following code, InnerClass is private in OuterClass.
namespace ClassTest
{
class OuterClass
{
private class InnerClass
{
}
OuterClass()
{
InnerClass Test = new InnerClass();
}
}
class Program
{
static void Main(string[] args)
{
OuterClass TestOne = new OuterClass();
InnerClass TestTwo = new InnerClass(); // does not compile
}
}
}
What are the Default Access Modifiers in C#?
and there is a private class but in my opinion its pointless
This question already has answers here:
Adding a set accessor to a property in a class that derives from an abstract class with only a get accessor
(3 answers)
Closed 9 years ago.
So I have an abstract class called AbstactSearchWithTwoLevelCache that was provided to me. All of its abstract properties only have read access (with a get;). I am not permitted to add a set; to the those abstract properties. Is there a way to change this in the derived class, SearchWithTwoLevelCache? In other words, is there a way to set these properties in the derived class?
If you mark the property with new, you define a new property, like this:
abstract class BaseClass
{
public int Property
{
get { ... }
}
}
class NewClass : BaseClass
{
public new int Property
{
get { return base.Property; }
set { ... }
}
}
EDIT:
The above works if the property in the base class is not abstract. When it is abstract, this will not work since you need to implement it. One option you do have is to create a class in between, like this:
abstract class BaseClass
{
public abstract int Property { get; }
}
class Between : BaseClass
{
public override int Property
{
get { ... }
}
}
class NewClass : Between
{
public new int Property
{
get { return base.Property; }
set { ... }
}
}
This however in no way is an elegant solution. Then, the real answer becomes that you cannot really do this (at least not without the above work around).
I can't imagine a way you can override them directly. Indirectly when you override the abstract class properties, the Getters can retrieve from a private field, and then you can create new properties that have Setters that set those fields.
Kind of a rig I know. Maybe there is a more elegant way around this.