C# Language
* Understanding and appropriately using access modifiers is crucial for designing robust and maintainable object-oriented code in C#.
What are access modifiers in OOP?
In object-oriented programming (OOP) with C#, access modifiers are keywords used to specify the accessibility of classes, methods, properties, and other members within a program. These access modifiers control the visibility and accessibility of members from other parts of the program.
• C# provides five access modifiers:
1. public: Members with this access modifier are accessible from any other code within the same assembly or from other assemblies that reference the assembly containing the public member.
2. private: Members with this access modifier are accessible only from within the same class or struct. They are not accessible from outside the containing type.
3. protected: Members with this access modifier are accessible within the same class, subclasses (derived classes), and other classes within the same assembly. They are not accessible from outside the assembly unless accessed through a derived class.
4. internal: Members with this access modifier are accessible within the same assembly but not from outside the assembly.
5. protected internal: Members with this access modifier are accessible within the same assembly and by derived classes, even if they are outside the assembly.
Here's a very basic example demonstrating access modifiers in a simpler program:
using System; public class Person { private string name; public int age; public Person(string name, int age) { this.name = name; this.age = age; } public void DisplayInfo() { Console.WriteLine($"Name: {name}, Age: {age}"); } } class Program { static void Main(string[] args) { Person person = new Person("Ayan", 28); // Error: 'Person.name' is inaccessible due to its protection level // person.name = "Suman"; person.age = 25; // age can be accessed and modified directly person.DisplayInfo(); } }
Name: Ayan, Age: 25
Explanation:
⤏ 'name' is a private field, so it can only be accessed and modified within the 'Person' class. Attempting to modify it directly outside of the class results in a compile-time error.
⤏ 'age' is a public field, so it can be accessed and modified from anywhere.
⤏ The 'DisplayInfo' method is public, allowing it to be called from outside the 'Person' class. It accesses both 'name' and age fields internally to display the person's information.
Note: This program creates a 'Person' object, sets its age, and displays its information. However, attempting to modify the person's name directly outside of the 'Person' class would result in an error due to the private access modifier on the 'name' field.
Resolve: Warning CS0169
The warning CS0169 in C# indicates that a private field has been declared but never used within the class. While this warning doesn't necessarily affect the functionality of the code, it suggests that the field might be unnecessary and could be removed to improve code readability and maintainability.
public class MyClass { private int privateField; } class Program { static void Main(string[] args) { myObject.privateField = 10; // Error: privateField is not accessible } }
• Address this warning:
⤏ Remove the unused private field: If the private field is truly not needed in the class, you can safely remove it.
⤏ Use the private field: If the private field serves a purpose but is simply not being used in the particular context, consider using it within the class, or refactor the code to utilize it.
⤏ Suppress the warning: If you believe that the unused private field is necessary for future use or for clarity, you can suppress the warning using compiler directives. However, this should be done judiciously, as it may hide potential issues or clutter the code with unnecessary information.
• Suppress the warning using pragma directives:
#pragma warning disable CS0169 private int privateField; // Suppress CS0169 warning for this field #pragma warning restore CS0169
⤏ Learn more about pragma directives in the C++ chapter.
Note: it's generally recommended to address warnings rather than suppress them whenever possible to maintain clean and easily understandable code.
What's Next?
We've now entered the finance section on this platform, where you can enhance your financial literacy.