Fields vs Properties in C#: What's the difference?

View profile for muhammad mohammad

Aspiring Full Stack Desktop Developer | C#, .NET Framework, WinForms | Strong in C++, SQL, ADO.NET | Passionate Problem Solver |Networking Engineer

  quick and simple way to understand the difference between Fields vs. Properties  in C#: Field: It's like a box where you directly store data inside a class. It’s just a variable inside the class. For example: public class Person { public string name; // this is a field } Property: It looks like a field from outside but actually has special methods (called getters and setters) behind the scenes to control how you read or write the value. It’s safer and more flexible. For example: public class Person { private string _name; // private field public string Name // property { get { return _name; } set { _name = value; } } } Why use properties? Because they let you add rules when getting or setting data, like checking values or triggering actions — fields don’t allow that. In short: Field = simple data storage, directly accessible Property = controlled access to data with get/set methods Hope that helps!

  • diagram

To view or add a comment, sign in

Explore content categories