LFInteractive Docs
  • Programming Documentation
  • C#
    • Installing Visual Studio
    • Understanding C#
      • Namespaces
      • Classes
      • Enum
      • Variables
        • Static Types
        • Primitive Types
        • Access Modifiers
        • Variables vs Properties
        • Nullable Variables
      • Getters and Setters
      • Solution vs Project
      • Struct vs Class
      • Coding Conventions
      • Tasks and Async
        • Parallel Tasks
      • Methods
      • PreProcessor Statements
    • Creating Your First Console App
      • Class Library
        • Models
          • File Model
          • Result Model
        • Controllers
          • File Controller
          • File System Controller
      • Console App
        • Nuget Packages
        • Main Method
    • Minecraft.NET
      • Minecraft.NET Library
      • Modrinth.NET Library
      • CurseForge.NET Library
      • Fabric.NET Library
    • Common Lib
      • Strings
      • Advanced Network Client
      • Advanced Timer
      • Advanced File Info
      • Configuration File
      • Application Config
      • Database File
      • Crypt Class
  • C++
    • Networking
      • Windows Socket (Client)
    • cclip
    • VCPKG
    • spdlog
      • Getting Started
      • Patterns
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. C#
  2. Understanding C#
  3. Variables

Variables vs Properties

In C#, properties and variables serve different purposes and have distinct characteristics. Here's an explanation of the difference between properties and variables:

Variables:

  • Variables are used to store and hold values in memory.

  • They are simple storage locations and do not have any additional logic associated with them.

  • Variables can be accessed directly to read or modify their values.

  • Variables can have different data types such as integers, strings, booleans, etc.

Here's an example of using variables in C#:

int age; // Variable declaration
age = 25; // Variable assignment
Console.WriteLine(age); // Accessing variable to read its value
age = 30; // Modifying the variable
Console.WriteLine(age); // Accessing modified value

Properties:

  • Properties are a way to encapsulate the access to private data members (fields) of a class by providing controlled access via getter and setter methods.

  • They provide a layer of abstraction and enable additional logic, such as validation or computation, to be executed when reading or setting values.

  • Properties are accessed syntactically like variables, but internally, they are associated with methods called getters and setters.

Here's an example of using properties in C#:

public class Person
{
    private string name; // Private field

    public string Name // Property
    {
        get { return name; } // Getter method
        set { name = value; } // Setter method
    }
}

Person person = new Person();
person.Name = "John"; // Setting the property value
Console.WriteLine(person.Name); // Accessing the property value

In the above example, the Name property provides controlled access to the private name field. The getter (get) retrieves the value of the field, and the setter (set) sets the value. By using the property, external code interacts with the private field indirectly, enabling additional logic to be executed if needed.

Properties provide advantages such as data encapsulation, abstraction, and the ability to control access to data members. They allow you to enforce validation rules, calculate derived values, trigger events, and provide a more consistent and maintainable interface for working with the underlying data.

PreviousAccess ModifiersNextNullable Variables

Last updated 1 year ago

Was this helpful?