The World Of C-Sharp 2


Introduction


This blog is part 2 of my previous blog - The World of C-Sharp 1 - and if you haven’t seen that yet, be sure to check it out. In this blog, I will be going through Storing Classes, Fields, and Constructors and introducing APIs specifically [HttpPost].


Storing Classes


In C-Sharp, storing a class usually means saving the state of an object (an instance of the class) to some form of persistent storage like a file, database, or memory. To store a class is fairly simple; first, you need to create a class that you want to store:

namespace NewASPCOreAPI.Models;
public class RndNumber
{
    public DateTime GeneratedAt { get; private set; }
    public int Number { get; private set; }

    public RndNumber()  
    {
        GeneratedAt = DateTime.Now;
        Number = new Random(0).Next(100);
    }
}

Then you can refer to this class in a different file

var x = new RndNumber();

In this example, I have a class called RndNumber, which contains many properties such as DateTime and Number, so in a different file, I first started by using a variable. The reason for using a variable is to be able to stash the data to use later, and after this, use whatever name you want; here I’ve used x. Finally, instantiate the class as a method.

This can be useful for you; for example, you could harness the properties of this class, like Number or DateTime, and use them somewhere else in your code.


Fields


Fields are extremely common to properties except a whole lot simpler. Fields like properties are ways of storing data but to define a field all you must do is define the type of field it is and then the name, no get or set is required.

String name
int number

Constructors


Constructors are special methods in a class that are automatically called when an instance or object of that class is created. The purpose of a constructor is to initialize objects by setting initial values for that property

public RndNumber()
{
    GeneratedAt = DateTime.Now
}

Using the previous example of RndNumber, this example of a constructor initialises the property GeneratedAt to DateTime.Now. Essentially, what this is doing is it allows for private sets to be changed inside the class


APIS - HttpPost


Apis are one of many ways to retrieve data from a given command and are used for the backend of the spectrum. In C-Sharp, APIs are the foundation of any project and often allow you to connect these APIs to different applications, such as E-commerce. In a nutshell, APIs assist with organisation and data retrieval. In this blog, I will only be covering HttpPost, but in future blogs, I will go over other ones.

HttpPost is one of many but allows you to create.

[HttpPost]

First, to define a HttpPost, simply write HttpPost in square brackets.

Next, for this blog, I will be creating a create number API. Create an async Task with the following:

public async Task<IActionResult> Numbers([FromBody] CreateNumber request)

Unpacking this code, we start with a basic async function, with Task<IActionResult>. Throughout your C-sharp coding experience, you will be using this quite often. Next, define the name. In this example, it is Numbers, following that, we want to get properties from that body, which is what [FromBody] does. Next, use the file that you want the information from and add request after that.

_numbers.Add(new StoredNumber
{
    Id = Guid.NewGuid(),
    Number = request.Number,
    CreatedAt = DateTime.Now,
});

Next, use the -Numbers variable. If you are wondering what _numbers is, basically I have created a static variable for a class to make my life easier:

static List<StoredNumber> _numbers = new List<StoredNumber>();

After using the keyword Add, meaning add, instantiate a class that I have already created, but this class has information that this new number has to include.

namespace NewASPCOreAPI.Models
{
    public class StoredNumber   
    {
        public Guid Id { get; set; }
        public int Number { get; set; }
        public DateTime CreatedAt { get; set; }
    }
}

Then, with open curly brackets, list the things this number must include using the StoredNumber class.

Then that’s it; let’s save and check it out on the web.

You will notice a Post request on your screen. Opening this you can see a whole lot. Click the try it out button and insert your number.

Once pressing the execute button the number you just created will show and the id as well as the date it was created at.


Conclusion


In conclusion, this blog aimed to help you understand a few more key components and basics of C-sharp, such as fields, constructors, storing classes, and a touch on APIs. In the next blog, I will dive deep into more APIs, such as HTTP (get, put and delete), and show you each functionality.