diff --git a/Ical.Net/CalendarObject.cs b/Ical.Net/CalendarObject.cs index 0dcfd888..b0190d9f 100644 --- a/Ical.Net/CalendarObject.cs +++ b/Ical.Net/CalendarObject.cs @@ -3,6 +3,7 @@ // Licensed under the MIT license. // +#nullable enable using System; using System.Runtime.Serialization; using Ical.Net.Collections; @@ -14,8 +15,9 @@ namespace Ical.Net; /// public class CalendarObject : CalendarObjectBase, ICalendarObject { - private ICalendarObjectList _children; - private ServiceProvider _serviceProvider; + // Are initialized in the constructor + private ICalendarObjectList _children = null!; + private ServiceProvider _serviceProvider = null!; internal CalendarObject() { @@ -50,11 +52,11 @@ private void Initialize() protected virtual void OnDeserialized(StreamingContext context) { } - private void Children_ItemAdded(object sender, ObjectEventArgs e) => e.First.Parent = this; + private void Children_ItemAdded(object? sender, ObjectEventArgs e) => e.First.Parent = this; protected bool Equals(CalendarObject other) => string.Equals(Name, other.Name, StringComparison.OrdinalIgnoreCase); - public override bool Equals(object obj) + public override bool Equals(object? obj) { if (ReferenceEquals(null, obj)) return false; if (ReferenceEquals(this, obj)) return true; @@ -89,7 +91,7 @@ public override void CopyFrom(ICopyable c) /// /// Returns the parent iCalObject that owns this one. /// - public virtual ICalendarObject Parent { get; set; } + public virtual ICalendarObject? Parent { get; set; } /// /// A collection of iCalObjects that are children of the current object. @@ -99,18 +101,18 @@ public override void CopyFrom(ICopyable c) /// /// Gets or sets the name of the iCalObject. For iCalendar components, this is the RFC 5545 name of the component. /// - public virtual string Name { get; set; } + public virtual string? Name { get; set; } /// /// Gets the object. /// The setter must be implemented in a derived class. /// - public virtual Calendar Calendar + public virtual Calendar? Calendar { get { ICalendarObject obj = this; - while (obj is not Net.Calendar && obj.Parent != null) + while (obj is not Net.Calendar && obj?.Parent != null) { obj = obj.Parent; } @@ -140,9 +142,9 @@ public virtual Calendar Calendar public virtual void RemoveService(string name) => _serviceProvider.RemoveService(name); - public virtual string Group + public virtual string? Group { get => Name; set => Name = value; } -} \ No newline at end of file +} diff --git a/Ical.Net/CalendarObjectBase.cs b/Ical.Net/CalendarObjectBase.cs index 8d3cbcda..34d60a0a 100644 --- a/Ical.Net/CalendarObjectBase.cs +++ b/Ical.Net/CalendarObjectBase.cs @@ -3,12 +3,12 @@ // Licensed under the MIT license. // +#nullable enable using System; namespace Ical.Net; -// This class should be declared as abstract -public class CalendarObjectBase : ICopyable, ILoadable +public abstract class CalendarObjectBase : ICopyable, ILoadable { private bool _mIsLoaded = true; @@ -17,20 +17,18 @@ public class CalendarObjectBase : ICopyable, ILoadable /// to the current object. This method must be overridden in a derived class. /// public virtual void CopyFrom(ICopyable obj) - { - throw new NotImplementedException("Must be implemented in a derived class."); - } + => throw new NotImplementedException("Must be implemented in a derived class."); /// /// Creates a deep copy of the object. /// /// The copy of the object. - public virtual T Copy() + public virtual T? Copy() { var type = GetType(); var obj = Activator.CreateInstance(type) as ICopyable; - if (obj is not T objOfT) return default(T); + if (obj is not T objOfT) return default(T?); obj.CopyFrom(this); return objOfT; @@ -38,11 +36,11 @@ public virtual T Copy() public virtual bool IsLoaded => _mIsLoaded; - public event EventHandler Loaded; + public event EventHandler? Loaded; public virtual void OnLoaded() { _mIsLoaded = true; Loaded?.Invoke(this, EventArgs.Empty); } -} \ No newline at end of file +}