Monday, June 12, 2023

Implementing INumber

 I thought it might be useful for some to describe my tool set. I am currently using Visual Studio Community 2022, I typically keep it up to date. I am using .NET 7.0 and C# 11.0. I use Git for version control. I'm using GitHub for storage of the repository and management of the project. I am also currently experimenting with GitHub Copilot for code suggestions and assistance.

My intention is to create a branch for each blog post or perhaps group of blog posts, depending on how tasks, or ideas get broken up. I will then try to check in every major step of the way. The banch for today is, ImplementINumber.

First I will add a Value. because a number has a value. This is not part of the interface, but may be used by the interface. I am using a property because it is a practice that I am used to and is common in the offices that I have worked in. Specifically using what is know as an auto property. Technically any data stored in a class is a field, a property is a set of methods that set the data and get the data. Using methods for access allows for validation and alteration as needed either on setting or getting the data. A standard property is written - 

private int _value;
private int Value
{
  get { return _value; }
  set { _value = value; }
}

I realize that I am explaining things in a haphazard manor. If you feel that I'm being too detailed feel free to skip any description, paragraph, post or the whole blog. If you feel that there is something that you need explained further, please feel free to ask, if I cannot answer I may be able to help you find the answer.

Returning to properties, the previous code snippet is a fairly standard property/field code snippet. If only there were a simpler way to write it that functionality in .NET would be able to replace with the structure above in the background. Well, there is -

private int Value { get; set; }

The backing field is assumed, as is the code for the get and the set. This format does allow for the declaration of the get or set, if it is needed. This is what is known as an auto property.

At this point Quick Actions can be used to create all the interface functions. It seems that there were some limitations on the Quick Actions. But there are other Quick Actions to add more for the interface. I may want to make value protected and not private. I may even end up making my own interface to add properties. But I will finish making the int number first.

I feel like I've lost the plot. I need to reset.

No comments:

Post a Comment