How to hide method so you can't access?

I want to hide a method so that it is not accessible from an outside of the class.
public class MyList : List<object>
{
	//optional method
	public new void Insert( int index, object item )
	{
	}
}
 
public class MyClass
{
	public MyClass()
	{
		MyList ml = new MyList();
 
		//I want Insert to be hidden, so I can't access it
		//ml.Insert();
	}
}

Open in new window

HOVEAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RishadanPortCommented:
declare the method as private.
RishadanPortCommented:
If though you are overriding a method, the method must be declared as public.  To overcome this, you can put another wrapper class around the current class, and label the inner class as private

Example:
//Shell class has access to the MyList class. Doing this will hide the Insert method from non Shell objects
public class Shell{
   private class MyList : List<object>
   {
	   //optional method
	   public new void Insert( int index, object item )
	   {
   	   }
   }
}

Open in new window

HOVEAuthor Commented:
Declaring method as private does not solve the problem, base Insert is still accessible.
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

RishadanPortCommented:

public class Shell{
   private class MyList : List<object>
   {
      //define all hidden methods here
   }
 
   private MyList list;
 
   public Shell(){
      list = new MyList();
   }
 
   //Define public methods here that the user can use. Do not
   //show the MyList class
}

Open in new window

RishadanPortCommented:
In the example above the Shell class will have access to the list variable, that is a MyList. However, when you initialize the Shell class, you cannot however use the Insert function, since the MyList class is hidden
RishadanPortCommented:
You could actually do something like this to make it even better:


public class Shell{
   private class MyList : List<object>, IAllowableFunctions{
      //define functions here
   }
   
   public interface IAllowableFunctions{
      //define functions u want to be declared public here
      void Add();
 
      void Remove();
   }
 
   private MyList list;
 
   public Shell(){
      list = new MyList();
   }
 
   //returns back a IAllowableFunctions interface that only
   //has access to the listed functions
   public IAllowableFunctions List{
      get{ return list; }
   }
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HOVEAuthor Commented:
Is there no other way, maybe with an attribute, to hide a method? Now I need to call Shell.List to access methods, otherwise I need to specify all methods in Shell class if I what them to bee accessed directly.
RishadanPortCommented:
That's a good question. I have come across this type of thing, but I don't no of another way around it. I will put something up if I figure something out
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.