Skip to content

Instantly share code, notes, and snippets.

String interpolation

var x = 1;

// same as string.Format("A {0} B", x) 
var y1 = $"A {x} B"; // y == "A 1 B"

// $@ for multiline
var y2 = $@"A
@aaronhoffman
aaronhoffman / Program.cs
Last active April 26, 2018 12:23
Custom Azure WebJob TraceWriter For SQL Server
static void Main()
{
var config = new JobHostConfiguration();
// Log Console.Out to SQL using custom TraceWriter
// Note: Need to update default Microsoft.Azure.WebJobs package for config.Tracing.Tracers to be exposed/available
config.Tracing.Tracers.Add(new SqlTraceWriter(
TraceLevel.Info,
"{{SqlConnectionString}}",
"{{LogTableName}}"));
// <copyright file="LeastRecentlyUsedCache.cs" company="http://www.sinbadsoft.com">
// Copyright (c) Chaker Nakhli 2013
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
// License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
// applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
// governing permissions and limitations under the License.
// </copyright>
// <author>Chaker Nakhli</author>
// <email>Chaker.Nakhli@sinbadsoft.com</email>
@contensis
contensis / mime_type_configuration.xml
Last active October 1, 2020 08:29
How to configure MIME types within web.config.
<?xml version="1.0"?>
<configuration>
<system.webServer>
<staticContent>
<mimeMap fileExtension=".vtt" mimeType="text/vtt" />
<mimeMap fileExtension=".srt" mimeType="text/srt" />
<mimeMap fileExtension=".aac" mimeType="audio/aac" />
<mimeMap fileExtension=".oga" mimeType="audio/ogg" />
<mimeMap fileExtension=".mp4" mimeType="video/mp4" />
<mimeMap fileExtension=".webm" mimeType="video/webm" />
@kristopherjohnson
kristopherjohnson / SHA1Util.cs
Last active October 1, 2020 22:18
.NET/C# Generate SHA1 hex string for a string encoded as UTF8
using System.Security.Cryptography;
using System.Text;
namespace Snippets
{
public static class SHA1Util
{
/// <summary>
/// Compute hash for string encoded as UTF8
/// </summary>
public class EmtpyStatementRemoval : CSharpSyntaxRewriter
{
public override SyntaxNode VisitEmptyStatement(EmptyStatementSyntax node)
{
//Simply remove all Empty Statements
return null;
}
}
public static void Main(string[] args)
@jcansdale
jcansdale / ExpressionUtilities.cs
Created September 9, 2016 17:31
Helper methods to quickly evaluate simple expressions
using System;
using System.Reflection;
using System.Collections.Generic;
using System.Linq.Expressions;
public static class ExpressionUtilities
{
public static object GetValue(Expression expression)
{
return getValue(expression, true);
@waf
waf / ArrayDeconstructionExtensions.cs
Created January 1, 2017 01:14
Add deconstruction (i.e. destructuring) syntax support for arrays for C# 7
using System;
using System.Linq;
namespace Extensions
{
/// <summary>
/// Allow the up to the first eight elements of an array to take part in C# 7's destructuring syntax.
/// </summary>
/// <example>
/// (int first, _, int middle, _, int[] rest) = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
@jamietre
jamietre / MethodInfoExtensions
Last active January 7, 2023 06:20
GetSignature extension for PropertyInfo and MethodInfo: returns the detailed method/property signature by reflection
public static class MethodInfoExtensions
{
/// <summary>
/// Return the method signature as a string.
/// </summary>
///
/// <param name="property">
/// The property to act on.
/// </param>
///
@tpeczek
tpeczek / CloudFlareConnectingIpExtensions.cs
Last active March 2, 2023 12:42
ASP.NET Core middleware for CloudFlare Connecting IP support
using System;
using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.HttpOverrides;
using Lib.AspNetCore.CloudFlareConnectingIp;
namespace Microsoft.AspNetCore.Builder
{
public static class CloudFlareConnectingIpExtensions
{
public static IApplicationBuilder UseCloudFlareConnectingIp(this IApplicationBuilder app)