One of the great things about C# is how you handle variables. Variables are statically typed, have access modifiers, and can even use a nifty shorthand trick to easily and flexibly create "global" variables without wordy getter/setter methods. Let's dive in to how variables work in C#!
Define a variable inside of a function, and it is only accessibly inside of that function. There is no var keyword in C#. Rather, as in Java, we write the data type of the variable, followed by the variable name, then a single equals sign and the value (optional when declaring the variable).
As in other languages, local variables are good for temporarily storing information for calculations and intermediate steps since they are easily garbage collected and don't hang around in memory.
Instance variables or field variables are available to the whole class. They are declared outside of any function in the class, and can use any of these access modifiers: public, private, or protected. Here is an example of the material variable that is accessible from anywhere inside of the class (but not to external classes):
Suppose you wanted an external class to have access to the material variable. We can do this quite easily in Visual Studio. If you had a private class level variable as in the example above, and you wanted to make is visible to other class then you could just right-click on the variable name and go to Refactor -> Encapsulate Field. Doing this generates the following code:
It's a good practice to create public variables like this because it prevents headaches later. Suppose later you decide that you want to change the name of the reference to the variable in the current class from _material to _originalMaterial. Now you can simply use ctrl + f to find and replace all occurrences in the file. Note that this won't break any references to the variable from other classes as they class still just access it with instanceVar.material. Without the encapsulation you would have to go to every class and change the variable name to the new name which is no fun, especially in big projects.
Static variables, of course, belong to the Class itself, and not instances of the class. It's quite simple in C# to create static variables. Static variables can have either public, private, or protected access modifiers just as other variables do. Here's a quick example where material is a static variable:
Suppose you want to make a variable that can be overridden by subclasses that extends from this class using inheritance. For example, with the code below you can have a function that accepts a Dog object and tries to access the .size property. If it is a Dog object, it will use the private _size property, but if it is a Beagle Object it will use the _size property from the Beagle class. This is normally the behavior you want, and here's how to do it:
Did I miss any C# variable types? Let me know what you think about this C# variables quick start guide!
The posts on this site are written and maintained by Jim Lynch. About Jim...