Skip to content

Commit

Permalink
C# language refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
axunonb committed Dec 13, 2020
1 parent 1961ab0 commit 40ffa54
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 46 deletions.
4 changes: 2 additions & 2 deletions Axuno.Tools/GeoSpatial/Angle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public static Angle Negate(Angle angle)
/// </returns>
public static bool operator ==(Angle angleA, Angle angleB)
{
return angleA?.Equals(angleB) ?? object.ReferenceEquals(angleB, null);
return angleA?.Equals(angleB) ?? angleB is null;
}

/// <summary>
Expand Down Expand Up @@ -458,7 +458,7 @@ internal static Tuple<string, int> ParseFormatString(string format)
++index;
}

return int.TryParse(format.Substring(index), NumberStyles.None, CultureInfo.InvariantCulture,
return int.TryParse(format[index..], NumberStyles.None, CultureInfo.InvariantCulture,
out var precision)
? Tuple.Create(format.Substring(0, index), precision)
: Tuple.Create(format, -1);
Expand Down
6 changes: 2 additions & 4 deletions Axuno.Tools/GeoSpatial/GoogleGeo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,8 @@ public class GeoResponse
/// <returns>Returns the <see cref="GeoResponse"/> from the Google API. If <see cref="GeoResponse.Success"/> the <see cref="GeoLocation"/> members will be set.</returns>
public static async Task<GeoResponse> GetLocation(string country, string address, string apiKey, TimeSpan timeout)
{
using (var client = new HttpClient())
{
return await GetLocation(client, country, address, apiKey, timeout);
}
using var client = new HttpClient();
return await GetLocation(client, country, address, apiKey, timeout);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Axuno.Tools/GeoSpatial/Latitude.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public override string ToString(string format, IFormatProvider formatProvider)
// negative sign is in the current format provider
var numberFormat = Angle.GetNumberFormatInfo(formatProvider);
var negativeSign = numberFormat.NegativeSign;
formatted = formatted.Substring(negativeSign.Length);
formatted = formatted[negativeSign.Length..];
}
return formatted + " " + Direction;
}
Expand Down
6 changes: 3 additions & 3 deletions Axuno.Tools/GeoSpatial/Location.Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,18 +275,18 @@ private static Angle ParseIsoAngle(string value, int degreeDigits)
{
case 1: // sign only : value represents degrees
angle = Angle.FromDegrees(
double.Parse(value.Substring(1), CultureInfo.InvariantCulture));
double.Parse(value[1..], CultureInfo.InvariantCulture));
break;
case 3: // sign + MM : value is degrees and minutes
angle = Angle.FromDegrees(
int.Parse(value.Substring(1, degreeDigits), CultureInfo.InvariantCulture),
double.Parse(value.Substring(degreeDigits + 1), CultureInfo.InvariantCulture));
double.Parse(value[(degreeDigits + 1)..], CultureInfo.InvariantCulture));
break;
case 5: // sign + MM + SS : value is degrees, minutes and seconds
angle = Angle.FromDegrees(
int.Parse(value.Substring(1, degreeDigits), CultureInfo.InvariantCulture),
int.Parse(value.Substring(degreeDigits + 1, 2), CultureInfo.InvariantCulture),
double.Parse(value.Substring(degreeDigits + 3), CultureInfo.InvariantCulture));
double.Parse(value[(degreeDigits + 3)..], CultureInfo.InvariantCulture));
break;
default:
return null; // Invalid format
Expand Down
11 changes: 4 additions & 7 deletions Axuno.Tools/GeoSpatial/Location.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,8 @@ public sealed partial class Location : IEquatable<Location>, IFormattable, IXmlS
/// </exception>
public Location(Latitude latitude, Longitude longitude)
{
if (latitude == null) throw new ArgumentNullException(nameof(latitude));
if (longitude == null) throw new ArgumentNullException(nameof(longitude));

Latitude = latitude;
Longitude = longitude;
Latitude = latitude ?? throw new ArgumentNullException(nameof(latitude));
Longitude = longitude ?? throw new ArgumentNullException(nameof(longitude));
}

/// <summary>Initializes a new instance of the Location class.</summary>
Expand Down Expand Up @@ -82,7 +79,7 @@ private Location()
/// </returns>
public bool Equals(Location other)
{
if (ReferenceEquals(other, null)) return false;
if (other is null) return false;

return Altitude.Equals(other.Altitude) && Latitude.Equals(other.Latitude) &&
Longitude.Equals(other.Longitude);
Expand Down Expand Up @@ -317,7 +314,7 @@ private static double MetersToRadians(double meters)
/// </returns>
public static bool operator ==(Location locationA, Location locationB)
{
return locationA?.Equals(locationB) ?? ReferenceEquals(locationB, null);
return locationA?.Equals(locationB) ?? locationB is null;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Axuno.Tools/GeoSpatial/Longitude.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public override string ToString(string format, IFormatProvider formatProvider)
// negitive sign is in the current format provider
var numberFormat = GetNumberFormatInfo(formatProvider);
var negativeSign = numberFormat.NegativeSign;
formatted = formatted.Substring(negativeSign.Length);
formatted = formatted[negativeSign.Length..];
}

return formatted + " " + Direction;
Expand Down
45 changes: 17 additions & 28 deletions Axuno.Tools/GermanHolidays.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,8 @@ public GermanHoliday this[Id holidayId]
{
get
{
Predicate<GermanHoliday> holidayFilter = h => h.Id.Value == holidayId;
return Find(holidayFilter);
bool HolidayFilter(GermanHoliday h) => h.Id.Value == holidayId;
return Find(HolidayFilter);
}
}

Expand All @@ -265,8 +265,8 @@ public List<GermanHoliday> this[DateTime date]
{
get
{
Predicate<GermanHoliday> dateFilter = h => h.Date.Date.Equals(date.Date);
return FindAll(dateFilter).OrderBy(h => h.Date.Date).ToList();
bool DateFilter(GermanHoliday h) => h.Date.Date.Equals(date.Date);
return FindAll(DateFilter).OrderBy(h => h.Date.Date).ToList();
}
}

Expand Down Expand Up @@ -360,29 +360,16 @@ private DateTime GetAdventDate(int num)
// 4th Advent is the latest Sunday before 25th December
var firstChristmasDay = new DateTime(Year, 12, 25);

switch (firstChristmasDay.DayOfWeek)
return firstChristmasDay.DayOfWeek switch
{
default: // DayOfWeek.Monday
return firstChristmasDay.AddDays(-1).AddDays(-7 * (4 - num));

case DayOfWeek.Tuesday:
return firstChristmasDay.AddDays(-2).AddDays(-7 * (4 - num));

case DayOfWeek.Wednesday:
return firstChristmasDay.AddDays(-3).AddDays(-7 * (4 - num));

case DayOfWeek.Thursday:
return firstChristmasDay.AddDays(-4).AddDays(-7 * (4 - num));

case DayOfWeek.Friday:
return firstChristmasDay.AddDays(-5).AddDays(-7 * (4 - num));

case DayOfWeek.Saturday:
return firstChristmasDay.AddDays(-6).AddDays(-7 * (4 - num));

case DayOfWeek.Sunday:
return firstChristmasDay.AddDays(-7).AddDays(-7 * (4 - num));
}
DayOfWeek.Tuesday => firstChristmasDay.AddDays(-2).AddDays(-7 * (4 - num)),
DayOfWeek.Wednesday => firstChristmasDay.AddDays(-3).AddDays(-7 * (4 - num)),
DayOfWeek.Thursday => firstChristmasDay.AddDays(-4).AddDays(-7 * (4 - num)),
DayOfWeek.Friday => firstChristmasDay.AddDays(-5).AddDays(-7 * (4 - num)),
DayOfWeek.Saturday => firstChristmasDay.AddDays(-6).AddDays(-7 * (4 - num)),
DayOfWeek.Sunday => firstChristmasDay.AddDays(-7).AddDays(-7 * (4 - num)),
_ => firstChristmasDay.AddDays(-1).AddDays(-7 * (4 - num))
};
}

/// <summary>
Expand Down Expand Up @@ -622,8 +609,10 @@ where holiday.Name.ToString().ToLower() == "holiday"
while (dateFrom <= dateTo)
{
var tmpDateFrom = new DateTime(dateFrom.Ticks);
var germanHoliday = new GermanHoliday(holidayId, holidayType, name, () => tmpDateFrom);
germanHoliday.PublicHolidayStateIds = germanFederalStateIds;
var germanHoliday = new GermanHoliday(holidayId, holidayType, name, () => tmpDateFrom)
{
PublicHolidayStateIds = germanFederalStateIds
};
switch (action)
{
case ActionType.Merge:
Expand Down

0 comments on commit 40ffa54

Please sign in to comment.