Introduction To C-Sharp
For starters, C-Sharp is an object-oriented language, meaning it revolves around classes, objects, etc. Being used by millions of people, C-Sharp has made a name for itself with beneficial perks such as cross-platform support, language interoperability, and great performance, making it an easy, quick, and engaging language to start off with whether you are just getting started or are willing to learn a different language.
What Can I Build With C-Sharp?
Unlike many other languages, such as Python, C-Sharp is known for developing in many different branches, these include:
Desktop Application Development
Windows Application
Cross-platform desktop apps
Web Development
- ASP.net Core
Game Development
Unity
VR/AR
Mobile Application Development
- .Net Maui
Backend Development
- Grapgql Apis
Basics To C-Sharp
For this blog, I will dive into the basics of C-Sharp, which will include:
Basic functionalities
Variables
Classes
Properties (Public and Private)
Basic Functionalities
In your visual studio, you will notice a bunch of stuff pop up onto your screen. Don’t be alarmed, let me take you through the basic components of your workspace from top to bottom.
First, we have the uppermost section of Visual Studio. Otherwise known as your “taskbar “, you will find your basic functionalities such as file, view, project, etc. Another important key feature in the taskbar is the play button. The play button will be one of your go-to things to press, as it is the main aspect of what your code produces.
Next, we have your solution explorer. Like Visual Studio code, if you have used it before, the solution explorer is where your files get created or deleted, and is the foundation of your project. It allows you to select which file you want to edit and control. Now, when it comes to file management, there will be 2 main folders we will be looking at. One will be the controller’s folder, and the next will be the models folder, for which I will take you through the process of creating these folders.
Next on the list is your output. For now, you will not have to worry about the output tab, but what the output is, is that it allows you to observe what is happening when either running your project or if you issue a command.
Finally, this section is where you write the code. Simply double-click on one of the files from the solution explorer or create one, and this display will pop up, allowing you to write code for your project.
These are all of the basic functionalities you will need to know to understand each section, which will ultimately assist in your coding journey, not just in Visual Studio, but in other coding editors.
Variables
What Are Variables?
Variables are a global term throughout the coding world due to their importance and usability in almost every single programming language. Variables are essentially ways of storing information, whether it be text, numbers, or dates and times. Different languages have different ways of declaring a variable, for example, in react to create a variable, you must state const and then follow it with your variable name.
Variables In C-Sharp
Creating a variable in C-Sharp is quite straightforward. First state the word var which mean variable, then the name of the variable. Finally, use the equals sign (=) to say that you want to set this variable to the information of whatever comes after the equals sign
var age = 1
Now as you can see in this example, we specify var then the name of that variable which is age, and then use the equals sign to assign this variable a value which in this case is the number 1.
Classes
What Are Classes?
Classes in C-Sharp are like a blueprint for creating objects, capturing data and behaviour. Classes define properties and methods that describe the characteristics and actions of an object. Don’t get mixed up with classes and objects, as they are different. Objects are instances of a class, created using the class as a template.
Classes In C-Sharp
Classes in C-Sharp are very common and useful. An example of a class in C-Sharp is:
namespace NewASPCOreAPI.Models
{
public class StoredNumber
{
public Guid Id { get; set; }
public int Number { get; set; }
public DateTime CreatedAt { get; set; }
}
}
In this example, we can see a whole bunch on the screen. To define a class in C-Sharp, simply type public class and the name of that class. Here the class is StoredNumber, and inside of the class, we have a bunch of properties.
Properties
What Are Properties?
Unlike variables, properties don’t have to have a set value from the start. With variables being static in their folder, properties can be defined and read from outside the file. Properties have a public or private state, whether you want them to be read from outside the file or just in that class. After stating your state, you set the property a type. This type can be anything that the library has, from, Strings to ints to DateTime. Next, you just state the name of this property then whether it can be changed or read through the get; set;.
Properties In C-Sharp
Creating a basic property in C-Sharp consists of 4 main parts:
Public Or Private - Defining whether your property is public or private is crucial. Public allows your property to be accessed outside of the file. Private restricts the property to that class, meaning you can only use it there and no-where else
Type - The next step is to set the type of the property. Whether it be string, int or DateTime.
Name - Next, you must give the property a name.
Get and Set - Finally, you must put {get; set;} at the end of the property. What this does is it tells Visual Studio that this property can be read and changed. Without these, your property will be unable to be used at all
Here are some examples of properties in C-Sharp:
public DateTime GeneratedAt {get; set;}
public DateTime GeneratedAt {get; private set;}
private int Number {get; set;}
string Name {get; set;}
In this example, I will show you 4 types of properties.
This first property is called GeneratedAt and its type is DateTime, it is also public meaning it is able to be used outside of this file
Although you may think 1 and 2 are the same, they are not. In the get and set, you might have noticed the private set. Essentially, making the set private tells Visual Studio that this property should not be able to change.
This property is called Number and is of type int; unlike the other properties, this one is private, making it only useful inside of its class.
This last property may confuse you. As you can see, its name is Name and is of type string, but unlike the others, there is no public or private being set. In C-Sharp if there is no public declaration, this property automatically becomes private.
These are the basic concepts of properties.
Overview - Conclusion
Lets down what we learnt today:
public class Product
{
public Int Price
public String Name
var ProductPrice = 10
var ProductName = Hat
}
Class Classes are defined with the word class followed by the name. In this example, Product is the class.
Property/s - The Properties in this example are Price and Name.
Variable - Variables are defined with the keyword var and in this case, the variables are ProductPrice and ProductName.
In the next blog, I will be going over Storing Classes, Fields, Constructors and introducing APIs specifically [HttpPost].