C Sharp Basics: Part 2: Public and Private Variables

C Sharp Basics:

C# is a multi-paradigm programming language created by Microsoft as part of the .Net initiative in 2000. It has gone on to have its own standard and become part of everything from desktop applications to used extensively in game development.


Public and Private Variables

Screenshot 2018-10-09 11.59.30

Objects can have public and private members. As discussed in the previous section, C# expects objects. In describing these objects and its relationships, they have data only they have access to, data other objects can use, or combinations of the two.

C# supports many different types of data types. From the more common int (integers; whole numbers) to string (collection of letters, numbers, and other symbols) and even the more complex List (sequence of data), there are lots of different types. However, all of them fall into two larger categories: public or private.

Public

When a variable or other object member is public, it can be accessed through the name of the object. It is public to other objects in this way. When thinking about the design of objects, a public member will be something another object would need or want to use.

Private

When an object member is private, only the object can use or access that data or functionality. It is private to the object itself. Often, it is useful to have data only the object needs have it it to private to the object itself.

Public and Private Methods

Screenshot 2018-10-09 12.09.24

The public and private keywords do not just apply to just variables. It applies to methods, sets of instructions, as well. Like variables, if a method is public, it can be accessed through the name of a object. Private members, however, cannot normally be accessed outside the object.

One way to pass information from one object to another is through public methods. These can be used through methods that process, transform, or simply return some value.

Note: Using a public method to return a private variable is usually not recommended, but works for this example.

Screenshot 2018-10-09 12.15.39

Once a new object is created using its description, public members can be accessed through using their name and the dot notation. Both public methods and variables can be accessed through using using the object, a period, and some public method or variable.

Public Methods and Public Variables

Screenshot 2018-10-09 12.19.46

Sometimes, it can be confusing if a object member is a variable or a method. Nearly always, the difference is in how it is used. A method will have opening and closing parentheses. A variables will not. Both can be accessed, however, through the object name.

Play with the example on Repl.It!

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s