What is the difference between a field and a property in C#?
Hello, colleagues! I'm a beginner C# developer and I can't understand the difference between a field and a property. They both seem to store data, so why were properties invented?\n\nHere I have a class `Person`. I can write like this:\n\n```csharp\npublic class Person\n{\n public string Name;\n}\n```\n\nAnd this is a field. But I see many people write like this:\n\n```csharp\npublic class Person\n{\n private string name;\n public string Name\n {\n get { return name; }\n set { name = value; }\n }\n}\n```\n\nAnd this is a property. Both options seem to work the same way: I can assign a value and read it. For example:\n\n```csharp\nPerson p = new Person();\np.Name = "Alexey";\nConsole.WriteLine(p.Name);\n```\n\nThe result will be the same. So why bother with `get` and `set`? I
No answers yet.
Log in to leave an answer