

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Private Methods in C#
Private Methods can only be used inside the class. To set private methods, use the private access specifier.
Private access specifier allows a class to hide its member variables and member functions from other functions and objects. Only functions of the same class can access its private members. Even an instance of a class cannot access its private members.
The following is an example −
Example
using System; class Demo { private int displayOne() { return 10; } public int displayTwo() { return 10; } } class Program { static void Main() { Demo d = new Demo(); // Console.WriteLine(d.displayOne()); Console.WriteLine(d.displayTwo()); } }
In the above example, we cannot call the private method displayOne(). If we try to call, then an error would be visible.
- Related Questions & Answers
- Private and final methods in C#
- Private Methods in Java 9 Interfaces
- Can we override private methods in Java
- Private and final methods in Java Programming
- Can I overload private methods in Java?
- @SafeVarargs annotation for private methods in Java 9?
- Can we use private methods in an interface in Java 9?
- What is the drawback of creating true private methods in JavaScript?
- Are the private variables and private methods of a parent class inherited by the child class in Java?
- Why do we need private methods in an interface in Java 9?
- What are the rules for private methods in an interface in Java 9?
- What are the advantages of private methods in an interface in Java 9?
- Can private methods of a class be accessed from outside of a class in Java?
- How to access the private methods of a class from outside of the class in Java?
- Private Variables in C#
Advertisements