Join David Gassner for an in-depth discussion in this video Everything is an object, part of C# Essential Training.
- View Offline
- C# is an object-oriented language. This makes it similar to Java, C++ and many others. Not all object-oriented languages are the same. Some languages are mostly procedural and incorporate some object-oriented features. C#, however, is a strict object-oriented language. That means that all values are stored as objects or as members of objects. We'll be talking about the nature of objects later, but for now, we'll use this definition.
But in C#, everything belongs to something. Objects in C# are instances of things called classes and structures. Classes and structures are very similar to each other in that they both define member objects. For example, if you want to work with some data in C#, you would define it as a member of a class or a structure. Persistent data is stored in something called a field, which is defined as a member of the class or structure.
Fields are typically private to the class or structure, that is, their values can only be read by code within the class or structure. But you can then expose those values as properties that can be read and set by the rest of the application. And behaviors are also defined as members of classes or structures, encapsulated in methods. A method is just like a function in a non-object-oriented language, but like fields and properties, it's a member of a class or structure.
Everything is a part of a class or structure, and much of what you work with are instances of these classes or structures. These instances are the objects we're speaking of, and so that's why we say everything is an object in C#. Here's what the definition of a class looks like in C#. It starts with a namespace declaration. I'll talk about namespaces later but in brief, a namespace is a way of organizing your code.
By placing each class in a namespace, you can avoid conflicts in names or identifiers. So for example, you could have two classes of the same name in a single application, as long as they're members of different namespaces. In C#, namespaces are defined purely in code. Unlike say, in Java, which organizes classes in packages, you don't have to relate the namespace to a folder of the same name.
But many developers do follow this practice and it's supported by Visual Studio. It's just not required by the C# compiler. Within the namespace, you declare the class or structure, and you assign it a name. Names of classes in C# are typically spelled with Pascal casing. That means it starts with an initial uppercase character and then uses lowercase or CamelCase for the rest of the name. Within the class, you'll find the member fields and properties.
This class has a single method called Main. The reason we do that is because console applications always start with a Main method, spelled just like this. In a console application, your executable code is placed within the Main method. This code is writing some output to the console and then waiting for some input. In addition to the classes you write, your C# applications have access to the framework class library.
As I previously described, this library contains classes that are available for all application frameworks, and it's included in both the .NET and WinRT frameworks. Examples of such classes are file management tools, mathematical functions and other common operations. Here's an example. Let's say you wanted to execute a mathematical operation. In some languages, simple mathematical functions might be implemented as a member of the language itself or as a global function.
But in C#, these tasks are defined as part of a class called System.Math. System is the namespace and Math is the name of the class. Most common mathematical operations are defined in this class as something called a static method. A static method is called as a member of the class itself, and not from an instance of the class. The Math class also has some common values that are stored as fields, or constants.
So for example, if you wanted to call some functionality from the Math class, first you would add this line of code, using System.Math. That's like the import statement in Java. It means I want to use this class in my code and make it available to the compiler. Once I've declared the using statement, I can refer to any of the member fields or methods of the class. So if I wanted to get the value of the mathematical constant pi, that's a field called Math.PI.
And if I wanted to round a numeric value, I could use Math.Round, and then I could output that value or do some other operation. You'll find that a lot of the things you might want to do in an application, such as mathematical operations, working with files, communicating over the Internet, and so on, are already implemented in the framework class library. And so it's a good idea to get to know that library. There are a lot of namespaces and classes in it, and you can't learn it all at the same time, but as you need a particular class, take a look at the documentation.
You can diagram your classes using Unified Modeling Language, or UML. This UML diagram for example, shows that there are two fields in the Math class called E and PI, and then many methods. Again, most of them are implemented as static methods that are called directly from the class. As you get to know C# as a pure object-oriented language, you'll find that learning the library is as much a part of the process as the language itself.
You'll see that there are some parts of the library that you use all the time, and you'll memorize their functionality and syntax. And then there will be other parts that you only use occasionally, and you'll have to go back to the documentation to learn the details as you need them.
- Declaring and initializing variables with a variety of data types
- Exploring operators, expressions, constants, and enumerations
- Controlling flow with conditional code and loops
- Handling exceptions
- Managing data collections
- Creating custom classes
- Organizing classes by namespace
- Understanding inheritance