C# Fields and Properties

C# Code Snippets Fields and Properties


In this code snippet, we will learn how to implement fields and properties in C#.

Fields are just normal variables but declared directly inside of a class. Even though you can make fields public, you shouldn’t(use a property instead). Only use fields locally inside the class.

Properties provide us with a way to read from and write to our private fields. In other programming languages, you usually have to implement getter and setter methods for this. Using properties you have way more control over your fields. For example, you can add some code for input validation. This way you can ensure that a value will fit in a certain range of values before assigning it.

Let’s see how to use and implement fields and properties.


Resulting output:


Leave a Reply

Your email address will not be published.

The following GDPR rules must be read and accepted:
This form collects your name, email and content so that we can keep track of the comments placed on the website. For more info check our privacy policy where you will get more info on where, how and why we store your data.

Advertisment ad adsense adlogger