1

i developed some codes to test.i know : if i use internal method , we can hide my method in another namespace forexample:

public vs. internal methods on an internal class

how to hide Add method in Main program;if i ress dot(.) intellisense show me Add method in Base method. i dont want to see add method to derive a instance from MyClass1?


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyDb;

namespace BaseClassUygulamasi { class Program { static void Main(string[] args) { new MyClass1().Save("123456789", "ali.yilmaz"); } } }

namespace MyDb {

public  abstract class MyBaseClass
{
    private Dictionary<string, string> dic { get; set; }

    public MyBaseClass()
    {

    }

    internal void Add(string key, string value)
    {
        dic.Add(key, value);
    }
}


public class MyClass1 : MyBaseClass
{
    public MyClass1()
        : base()
    {

    }

    public void Save(string TCKimlikNo, string AdSoyAd)
    {
        base.Add(TCKimlikNo, AdSoyAd);
    }
}

}

Community
  • 1
  • 1
Penguen
  • 16,836
  • 42
  • 130
  • 205

3 Answers3

4

Make the Add method protected instead of internal.

Klaus Byskov Pedersen
  • 117,245
  • 29
  • 183
  • 222
3

If you want classes that inherit from your base class to have access to the Add(...) method, but nothing external to that, then you need to mark it as protected, such that:

public  abstract class MyBaseClass
{
    private Dictionary<string, string> dic { get; set; }

    public MyBaseClass()
    {

    }

    protected void Add(string key, string value)
    {
        dic.Add(key, value);
    }
}


public class MyClass1 : MyBaseClass
{
    public MyClass1()
        : base()
    {

    }

    public void Save(string TCKimlikNo, string AdSoyAd)
    {
        base.Add(TCKimlikNo, AdSoyAd);
    }
}

This way, only classes that inherit from MyBaseClass will be able to access Add(...).

Samuel Slade
  • 8,405
  • 6
  • 33
  • 55
0

I think you are wanting to use the Protected keyword instead of internal.

UnhandledExcepSean
  • 12,504
  • 2
  • 35
  • 51