What is the difference between a field and a property

0 votes
What is the difference between a field and a property in C#, and when should a field be used instead of a property?
Jun 11, 2022 in C# by pranav
• 2,580 points
105 views

1 answer to this question.

0 votes

The internal workings of a class should be hidden from the outer world, according to object-oriented programming concepts. When you disclose a field, you're essentially revealing the class's internal implementation. As a result, we wrap fields in Properties (or methods in Java) so that we can modify the implementation without disrupting code that depends on us. Because we can put logic in the Property, we can also do validation logic and other tasks if necessary. Autoproperties are a somewhat perplexing concept in C# 3. This allows us to define the Property and have the C#3 compiler create the private field for us.

public class Person
{
   private string _name;

   public string Name
   {
      get
      {
         return _name;
      }
      set
      {
         _name = value;
      }
   }
   public int Age{get;set;} //AutoProperty generates private field for us
}
answered Jun 21, 2022 by jyoti
• 1,240 points

Related Questions In C#

0 votes
1 answer

What is the difference between C# and .NET?

C# is a programming language, and.NET refers ...READ MORE

answered Jun 7, 2022 in C# by pranav
• 2,580 points
122 views
0 votes
0 answers

What is the difference between C# and .NET?

Could you please explain the distinction between ...READ MORE

Sep 26, 2022 in C# by krishna
• 2,820 points
60 views
0 votes
1 answer

What are the differences between C, C# and C++ in terms of real-world applications?

C is a bare-bones, straightforward, and clean ...READ MORE

answered May 30, 2022 in C# by rajiv
• 1,620 points
126 views
0 votes
0 answers

What are the differences between C#.net and Visual Basic.net

I have some VB.net expertise and would ...READ MORE

Jun 11, 2022 in C# by krishna
• 2,820 points
66 views
0 votes
1 answer

Examples of dynamic polymorphism in c#

Check the example of polymorphism below. We ...READ MORE

answered Jun 7, 2022 in C# by pranav
• 2,580 points
211 views
0 votes
1 answer

Why should I avoid using Properties in C#?

Jeff dislikes properties because they resemble fields, ...READ MORE

answered Jun 7, 2022 in C# by rajiv
• 1,620 points
151 views
0 votes
1 answer

How abstraction and encapsulation differ

They are, in my opinion, slightly different ...READ MORE

answered Jun 9, 2022 in C# by rajiv
• 1,620 points
208 views
0 votes
1 answer

Object Oriented Programming Basic Concept(C#)

Use this link https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers protected internal: The type or ...READ MORE

answered Jun 17, 2022 in C# by jyoti
• 1,240 points
92 views
0 votes
1 answer

What is LINQ and what does it do

LINQ or Language Integrated Query may refer to: a ...READ MORE

answered Jun 21, 2022 in C# by jyoti
• 1,240 points
85 views
0 votes
1 answer

What is typing discipline

Wikipedia's typing discipline relates to the type ...READ MORE

answered Jun 14, 2022 in C# by jyoti
• 1,240 points
159 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP