Public class, but private member variables?

Angela Marie-Daley

I'm still finding my feet with C# and have a quick question that has been bugging me for a while.

Say I write a class and define a property as follows:-

public class Employee
{
    string FirstName {get; set;}
}

class Program
{
    private static object GetTheEmployee()
    {
        return new Employee{ FirstName = "Joe" }
    }
}

Why is use of FirstName within the GetTheEmployee method inaccessible, but when I change the FirstName 'string' variable in the Employee class to 'public string' instead, it is accessible from the Program class.

I would have thought that if I declare the class' access modifier as public, then all variables within in the class will also be public by default?

Andy Korneyev

The fact you've declared class as public doesn't mean all members of class will be also public.

By default class members (fields, properties, methods and so on) has private access modifier, so if you haven't explicitly declared your property as public (or protected, internal and so on), it will be private.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Public class, but private member variables?

From Dev

Initializing private member variables of a class

From Dev

Should unit test member variables be private or public?

From Dev

Access private member variables through public methods

From Dev

Java - private inner class public member

From Dev

Accessing private member variables inside public member function

From Dev

How can I access public member of private package class?

From Dev

How can I access public member of private package class?

From Dev

Typescript: Difference between declaring class variables using private, public and nothing

From Dev

Why is PHP private variables got public when extended class

From Dev

C++ private class member variables don't appear in the documentation

From Dev

Why can't this public member function call decltype on a private struct member declared inside the class?

From Dev

Accessing C++ class public member function from private struct data member

From Dev

Why aren't we allowed to access a private static member outside class without public member function?

From Dev

Public constructor of a private class

From Dev

Class (private and public)

From Dev

java private class member

From Dev

Object as private member of a class

From Dev

C++ public method inherited from base class can not access private member variable in derived class

From Dev

Private/Public Variables in C#

From Dev

Understanding public/private instance variables

From Dev

returning reference to private vs public member

From Dev

Accessing Private Member Without Public Accessor in Java

From Dev

Understanding "public" / "private" in typescript class

From Dev

Public members in package private class

From Dev

Is a class by default public, private or internal

From Dev

Is a class by default public, private or internal

From Dev

Inner private class and public method

From Dev

Public class with a private functions by default

Related Related

  1. 1

    Public class, but private member variables?

  2. 2

    Initializing private member variables of a class

  3. 3

    Should unit test member variables be private or public?

  4. 4

    Access private member variables through public methods

  5. 5

    Java - private inner class public member

  6. 6

    Accessing private member variables inside public member function

  7. 7

    How can I access public member of private package class?

  8. 8

    How can I access public member of private package class?

  9. 9

    Typescript: Difference between declaring class variables using private, public and nothing

  10. 10

    Why is PHP private variables got public when extended class

  11. 11

    C++ private class member variables don't appear in the documentation

  12. 12

    Why can't this public member function call decltype on a private struct member declared inside the class?

  13. 13

    Accessing C++ class public member function from private struct data member

  14. 14

    Why aren't we allowed to access a private static member outside class without public member function?

  15. 15

    Public constructor of a private class

  16. 16

    Class (private and public)

  17. 17

    java private class member

  18. 18

    Object as private member of a class

  19. 19

    C++ public method inherited from base class can not access private member variable in derived class

  20. 20

    Private/Public Variables in C#

  21. 21

    Understanding public/private instance variables

  22. 22

    returning reference to private vs public member

  23. 23

    Accessing Private Member Without Public Accessor in Java

  24. 24

    Understanding "public" / "private" in typescript class

  25. 25

    Public members in package private class

  26. 26

    Is a class by default public, private or internal

  27. 27

    Is a class by default public, private or internal

  28. 28

    Inner private class and public method

  29. 29

    Public class with a private functions by default

HotTag

Archive