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 in C# by pranav
• 2,580 points
14 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 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 in C# by pranav
• 2,580 points
23 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 in C# by rajiv
• 1,620 points
29 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 in C# by krishna
• 2,540 points
12 views
0 votes
1 answer

What is the best C# to VB.net converter

Telerik has a solid SharpDevelop-based converter that ...READ MORE

answered May 30 in C# by rajiv
• 1,620 points
63 views
0 votes
1 answer

Examples of dynamic polymorphism in c#

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

answered Jun 7 in C# by pranav
• 2,580 points
31 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 in C# by rajiv
• 1,620 points
33 views
0 votes
1 answer

How abstraction and encapsulation differ

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

answered Jun 9 in C# by rajiv
• 1,620 points
23 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 in C# by jyoti
• 1,240 points
18 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 in C# by jyoti
• 1,240 points
18 views
0 votes
1 answer

What is typing discipline

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

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