List Databases with a different collation than the SQL Server

select
    [d].[name] [Database Name]
   ,databasepropertyex( [d].[name], 'COLLATION' ) [Database Collation]
   ,serverproperty( 'COLLATION' ) [Server Collation]
from [sys].[databases] [d]
where ( databasepropertyex( [d].[name], 'COLLATION' )) != serverproperty( 'COLLATION' );

List collation on all columns in all tables in database on SQL Server

select
    concat( quotename( [s].[name] ), N'.', quotename( [t].[name] ), N'.', quotename( [c].[name] ) ) [Column_Name]
   ,[c].[collation_name] [Collation]
from [sys].[schemas] [s]
join [sys].[tables] [t] on [t].[schema_id] = [s].[schema_id]
join [sys].[columns] [c] on [c].[object_id] = [t].[object_id]
where [c].[collation_name] is not null
order by [Column_Name];

Create only 1 instance of an object per thread.

		/// <summary>
		///     Create only 1 instance of <see cref="T" /> per thread. (Only unique when using this method!)
		/// </summary>
		/// <typeparam name="T"></typeparam>
		public static class Cache<T> where T : notnull, new() {

			private static ThreadLocal<T> LocalCache { get; } = new(() => new T(), false);

			public static T? Instance { get; } = LocalCache.Value;

		}

Posted in C#

Some useful C# Swap methods

public static class SwapExt
{
	[MethodImpl(MethodImplOptions.AggressiveInlining)]
	public static void Swap<T>(ref T? left, ref T? right) => (left, right) = (right, left);

	[Pure]
	[MethodImpl(MethodImplOptions.AggressiveInlining)]
	public static (T? right, T? left) Swap<T>(this T? left, T? right) => (right, left);

	[Pure]
	[MethodImpl(MethodImplOptions.AggressiveInlining)]
	public static (T? right, T? left) Swap<T>((T? left, T? right) tuple) => (tuple.right, tuple.left);
}
Posted in C#

C# null coalescing operator, ??

C# has a nice ?? operator that can be chained.
In a statement, it will return the first non-null value or null if none are found to not be null.

For example, if you had the code:

string? value1 = null;
string? value2 = nameof(value2);
string? result = value1 != null ? value1 : value2;

The last line could be simplified down to:

string result = value1 ?? value2;

As far as I know, there is no limit on how many ?? can be chained!

Dream of Peace

I had a dream… A dream last night.
I saw the world in a different light.
A place to live without despair.
Where men can breathe a clearer air.

A joy like this, I’ve never known.
It filled my heart and filled my soul.

And the dream of peace lives on..
And the dream of peace lives on!

Protiguous, 2021
Note: This song may be from my childhood, but I have been unable to find it again.