none
The property that is seen but not inherited RRS feed

All replies

  • Could you get some example? What it means property is not inherited?
    Wednesday, April 4, 2018 6:26 AM
  • The property that is seen and inherited is public
    The property that is not seen and not inherited is private
    The property not seen and inherited is protected

    What property can be seen but not inherited



    Wednesday, April 4, 2018 6:37 AM
  • There may be some confusion of terminology here.

    The access modifier of a property determines whether it is accessible by any code outside of the class where it is defined (public), only accessible inside the defining type (private), accessible inside the defining type or any types that inherit from that type (protected).

    There are some other access modifiers as well such as internal and so on.

    Look here for details of the possible access modifiers you can use on a property.

    Wednesday, April 4, 2018 8:25 AM
  • Hi moham,

    I think you have some misunderstanding about the access level of properties and classes, about Accessibility Levels:

    public Access is not restricted.
    protected Access is limited to the containing class or types derived from the containing class.
    internal Access is limited to the current assembly.
    protected internal Access is limited to the current assembly or types derived from the containing class.
    private Access is limited to the containing type.
    private protected Access is limited to the containing class or types derived
    from the containing class within the current assembly. Available since
    C# 7.2.

    And classes can be inherited instead of properties, to determine if a class can be inherited depends on whether the class is sealed or not:

    Refer: Inheritance in C# and .NET

    Inheritance (C# Programming Guide)

    Abstract and Sealed Classes and Class Members (C# Programming Guide)

    Regards,

    Frankie


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 4, 2018 8:29 AM
  • Hello, I want to have a property in the class of the father who is seen in the child's class but not inherited
    Wednesday, April 4, 2018 12:38 PM
  • Classes can be inherited; there is no concept of 'inheriting' properties, as such.

    A property is either accessible from a child class or it is not. If you want a property defined in a parent class to be accessible in a child class that inherits from that parent, then that property must be declared as something other than private. For example, a protected property.

    public class Parent
    {
       protected string MyProperty {get;set;}
       private string SomePrivateProperty {get; set;}
    }
    
    public class Child : Parent
    {
        public void SomeMethod()
        {
           MyProperty = "Something"; // Allowed because MyProperty is protected, so I can access it.
    
           // SomePrivateProperty = "blah"; // Not allowed because private property not accessible here
        }
    }

    As a slight aside, the 'get' and the 'set' sides of a property can have different access levels (so you could have a property that has a public get, allowing any other code to retrieve the value, but a private set so only code inside the class can set it.

    Wednesday, April 4, 2018 12:57 PM
  • "The property that is seen but not inherited in c#"

    Can you provide a concrete example? I think the other posters are seeing this question the same way I am. You have some base type B that has members. Type D derives from B and can see members of B but not through inheritance. That isn't possible. The way a derived type "sees" the base type members is through inheritance. This is an oxymoron.

    public class Base
    {
       //"seen" in derived types
       public int Value1 { get; set; }
       protected int Value2 { get; set; }  
       protected virtual Value3 { get; set; }
      
       //Technically inherited but not seen in derived type
       private int Value4 { get; set; }
    }
    
    public class Derived : Base
    {
       //These properties are inherited and therefore seen
       // Value1
    
       //These properties are inherited and seen but not 
       //visible outside this type
       // Value2, Value3
    
       //These properties are inherited but not seen
       // Value4
    }

    However you may have a different concept in mind and are just mixing up terms. You can have a property on a derived type that isn't inherited from the base type. You simply add the property on the derived type.

    public class Derived : Base
    {
       //This member is added to the new type but not inherited
       //from the base type
       public int Value5 { get;set; }
    }

    You can also inherit a member but not allow derived types to override it. That is the default so you don't need to do anything special. But you can also inherit a member that was virtual but cannot be overridden anymore (sealed).


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, April 4, 2018 2:52 PM
    Moderator
  • You make it difficult because you don't write "where". 

    However, in this way I answer in fact your question. 


    Success
    Cor


    Wednesday, April 4, 2018 4:29 PM
  • You can create abstract Property or Method which will be inherited from base class but concrete class have to implement it. 

    It could be better create abstract get method because property is only get or set method. You can create field in concrete class and value will be get by abstract method. Each class could return field as you want. 

    Thursday, April 5, 2018 9:22 AM