
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
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 Articles
- Private and final methods in C#
- Private Methods in Java 9 Interfaces
- Private and final methods in Java Programming
- Can I overload private methods in Java?
- @SafeVarargs annotation for private methods in Java 9?
- Can we override private methods in Java\n
- 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 Destructor in C++

Advertisements