File size: 3,417 Bytes
93d826e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;

// Namespace declaration
namespace ExampleApp
{
    // Interface definition
    public interface IProcessor<T>
    {
        Task<T> ProcessAsync(T input);
        bool Validate(T input);
    }

    // Enum definition
    public enum Status
    {
        Pending,
        Active,
        Completed,
        Failed
    }

    // Delegate declaration
    public delegate void StatusChangedEventHandler(Status oldStatus, Status newStatus);

    // Generic class implementing interface
    public class DataProcessor<T> : IProcessor<T> where T : class
    {
        // Event declaration
        public event StatusChangedEventHandler StatusChanged;

        // Auto-implemented property
        public Status CurrentStatus { get; private set; }

        // Static field
        private static readonly Dictionary<Type, int> _processedItems = new();

        // Constructor
        public DataProcessor()
        {
            CurrentStatus = Status.Pending;
        }

        // Async method implementation
        public async Task<T> ProcessAsync(T input)
        {
            var oldStatus = CurrentStatus;
            CurrentStatus = Status.Active;
            OnStatusChanged(oldStatus, CurrentStatus);

            await Task.Delay(100); // Simulate work

            if (_processedItems.ContainsKey(typeof(T)))
                _processedItems[typeof(T)]++;
            else
                _processedItems[typeof(T)] = 1;

            CurrentStatus = Status.Completed;
            OnStatusChanged(Status.Active, CurrentStatus);

            return input;
        }

        // Interface method implementation
        public bool Validate(T input) => input != null;

        // Protected virtual method
        protected virtual void OnStatusChanged(Status oldStatus, Status newStatus)
        {
            StatusChanged?.Invoke(oldStatus, newStatus);
        }

        // Static method
        public static int GetProcessedCount<TItem>() where TItem : class
        {
            return _processedItems.GetValueOrDefault(typeof(TItem));
        }
    }

    // Record type (C# 9.0+)
    public record Person(string Name, int Age)
    {
        // Property with validation
        public string Email { get; init; } = string.Empty;
    }

    // Extension method
    public static class StringExtensions
    {
        public static int WordCount(this string str)
        {
            return str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Length;
        }
    }

    // Main program class
    public class Program
    {
        public static async Task Main(string[] args)
        {
            var processor = new DataProcessor<Person>();
            processor.StatusChanged += (old, @new) => 
                Console.WriteLine($"Status changed from {old} to {@new}");

            var person = new Person("John Doe", 30) { Email = "john@example.com" };
            
            if (processor.Validate(person))
            {
                var result = await processor.ProcessAsync(person);
                Console.WriteLine($"Processed person: {result.Name}");
                Console.WriteLine($"Word count in name: {result.Name.WordCount()}");
            }

            Console.WriteLine($"Total processed persons: {DataProcessor<Person>.GetProcessedCount<Person>()}");
        }
    }
}