Objects: Everything is an Object in C#

April 26, 2016
4 mins read

Microsoft Visual 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.

:: An object is an area of memory that stores data and behaviors.

C# doesn’t support the concept of global variables or functions that you might find in procedural languages. For example, in JavaScript in a browser, you can define a variable that’s global to the entire web page. And in Visual Basic 6, the predecessor to today’s Visual Basic for .NET, you could define variables that were global to an entire application.

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 talking 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 their 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.

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.

Data Scientist & Solution Architect || IBM Recognised Speaker, Mentor, and Teacher || Debater || Blogger || Guinness World Record Holder || Watson Solution Developer || IBM Community Activist || Aspiring to Inspire.

Leave a Reply

Your email address will not be published.

Previous Story

Create a Sample UWP App in C#

Next Story

Develop Your First UWP App Using JavaScript

Latest from Blog

Quantum Computing | What, Why & How

Now as we are starting the 5th decade after the very first proposed model in the 1980s, quantum computers are now commercially available, brought out of the lab, and into the industry by IBM Quantum. With many experts predicting it will revolutionize the way we approach problem-solving. With the recent

Case Study: Digital Transformation of “justSajid Bank”

In this current era of what we call “digital first”, industries are repeatedly changing and evolving with the help of technology. So as the banking industry is reshaping itself, banks need to keep adopting positive changes to counter and overcome the key challenges of the modern world to overcome the

Metaverse || A Whole New World.

Things are going to be super dramatic in the next few years. Metaverse is going to open a lot of new doors to revise things that we are doing now. The culture and lifestyle would be changed. Some new cultures would have emerged and everybody would be living without physical

Pakistan Day 2020 | Coronavirus Technical Solutions

Well, on Pakistan Day 2020 our nation along with the rest of the world is facing Corona Pandemic. As the sons of this great motherland, we are trying to play our roles at our places. Within the last few weeks, Alhamdulillah I’ve got a chance to develop two technical solutions

Password: Choose the Secure, Easy and Cool Password

A super secure Password is very common nowadays. Although according to experts the password is going to expire very soon. Moreover, the pin codes are also replacing password in some why. Perhaps still there is a lot of importance of a secure password in many places. The one of the most

JumpStart with DevOps

What is DevOps; DevOps is a Software Development Strategy, that bridges the gap between the Dev and the Ops side of the company. * It’s not a tool, it’s a Methodology to bridge the gap b/w development and operations teams! As there is always a lot of conflicts between the

JumpStart Programming with Python 3

Motivation; Well, if you want to play with data and deal with complex analytics problems then Python is the best for you. We can use Python for developing complex scientific and numeric apps. Python is designed with features to facilitate data analysis and visualization. The syntax in Python helps the programmers to do coding

Understanding of Data Science Methodology

Motivation: It’s all about the different methods used in data science. Data Science Methodology: There is the following methodology used in data science which can further categories into different phases; From Problem to Approach Business Understanding Analytical Approach Working with Data Data Requirements Data Collection Data Understanding Data Preparation Deriving the

Understanding Machine Learning Methodology

Motivation Well, if we talk about a human cell sample extracted from a patient. The cell would have some characteristics. One of the interesting questions we can ask, what kind of statistics that cell have? One could easily presume that only a doctor with years of experience could diagnose a tumor and

SIMPLE NAVIGATION IN WINDOWS APPLICATION

The Navigation of UWP Apps is extremely cool and easier than other platforms. It allows enabling a variety of intuitive user experiences for moving between apps, pages, and content. Navigation is the key part of the Application. In your small Apps, you may maintain your contents and functionality in a

UNDERSTANDING THE ARCHITECTURE OF UNIVERSAL WINDOWS PLATFORM

The architecture of Universal Windows Platform is most exclusive with the center of gravity which is One Windows Core. Now the whole development will follow the common refactored core that will common for all the windows Releases. It’s all about One Core, One Hardware Platform, Universal Hardware Driver and Standalone

Guinness World Record Holder | Hajj Hackathon | Khadim Al-Haramain Al-Sharifain

Khadim Al-Haramain Al-Sharifain Built an MVP of a cognitive platform name “Khadim Al-Haramain Al-Sharifain“.  Objective The objective was to facilitate Hajj operations by introducing cognitive services integrated with Hajj systems. Hence millions of Muslims who are there for Allah Rab-ul-Izat, can perform anything without disturbing their focuses and concentrations.  Functionalities

JumpStart with IBM Bluemix

IBM Bluemix is the world most exclusive cloud platform that provides developers to quickly develop, Deploy and manage Apps over the cloud without dealing with any underline infrastructure. Bluemix provides great open source platforms for your needs. Tech experts believe that up to 2020 the computing era would completely change.

JumpStart Into Big Data With HDInsight

What would happen when the volume of your data increased repeatedly over time and you need high velocity at the same time. Not only that but you have a different variety of data and Variability also exists in your data. So how would you handle all that data? If we

Microsoft SharePoint: Create an Intranet in SharePoint

As you should already have some hands on with SharePoint. So, Lets do some hands on with SharePoint Intranet for Small company. Simply start with a Blank Site Collection. Let’s assume that the company has 3 Departments: Sales, Production and Support. They each have their own area in which they should

.NET Core

.NET Core 1.0  .NET Core 1.0 is a major new investment in the future of .NET and laying the foundation for decades to come. Still, it is in its early stage, and for some time you might still focus .NET Framework 4.6 depending on your application needs. But for many scenarios,

JUMPSTART WITH DATA BINDING IN UWP

Data Binding is one of the most interesting topics in Universal App Development. In order to perform the better functionality, you really need to have some great usage of Data Binding in you Apps. So! First thing first. What is actually the Data Binding and is its usage in UWP.

justSajid | A PLACE FOR MY THOUGHTS

This blog is my personal mental playground. The views expressed are mine, and mine alone. They do not represent the views of my employer, my family, my friends, my imaginary friends, my neighbors, people who live in my city, province, country, region, or planet, nor do they represent the views

justSajid | A PLACE FOR MY THOUGHTS

justSajid is my personal mental playground. The views expressed are mine, and mine alone. They do not represent the views of my employer, my family, my friends, my imaginary friends, my neighbors, people who live in my city, province, country, region, or planet, nor do they represent the views of

Agentic AI: The Future of Machines Acting with Purpose

Artificial Intelligence (AI) has come a long way from being a fancy algorithm for chess games to becoming our digital assistant, creative collaborator, and even a philosopher’s muse. One of the more intriguing advancements in AI is Agentic AI—an AI system that can act autonomously, pursue goals, and make decisions,
GoUp