As a fresher, I am trying to learn C# online. I came across property and field concept and I got this doubt. The following two code snippets give the same output, so what is the difference. Please explain in easy words.
{
class Student
{
// Auto-implemented Properties
public int ID;
public string Name;
public string Email;
}
class AutoImplementedProperty
{
public static void Main(string[] args)
{
Student student = new Student();
// Setting properties
student.ID = 101;
student.Name = "Rahul Kumar";
student.Email = "rahul@example.com";
// Getting properties
Console.WriteLine(student.ID);
Console.WriteLine(student.Name);
Console.WriteLine(student.Email);
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
namespace CSharpFeatures
{
class Student
{
// Auto-implemented Properties
public int ID { get; set; }
public string Name { get; set; }
public string Email { get; set; }
}
class AutoImplementedProperty
{
public static void Main(string[] args)
{
Student student = new Student();
// Setting properties
student.ID = 101;
student.Name = "Rahul Kumar";
student.Email = "rahul@example.com";
// Getting properties
Console.WriteLine(student.ID);
Console.WriteLine(student.Name);
Console.WriteLine(student.Email);
Console.ReadLine();
}
}
}