C# Class Members

This post will quickly go through three main class members including property, method and event.

Three class members:

  • Property: Storeing data. Combining them can represent the statu of classes or objects.
  • Method: It is evolved from ‘function’. Methods have logic and complete tasks.
  • Event: A mechanism for classes or objects to communicate with other classes or objects.

Method Example

namespace MethodSample
{
    internal class Program
    {
        static void Main(string[] args)
        {
            double x = Math.Sqrt(4);
            Console.WriteLine(x);
        }
    }
}

Event Example

namespace EventSample
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DispatcherTimer timer = new DispatcherTimer();
            // Property
            timer.Interval = TimeSpan.FromSeconds(1);
            // Method
            timer.Tick += Timer_Tick;
            // Event
            timer.Start();
        }

        private void Timer_Tick(object? sender, EventArgs e)
        {
            this.timeTextBox.Text = DateTime.Now.ToString();
        }

        private void timeTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {

        }
    }
}