Last active
December 8, 2020 14:36
-
-
Save kostasgrevenitis/2afd270f3e7ba363b5fe3812091fc68f to your computer and use it in GitHub Desktop.
"C# extension methods to save the day" article - 29/11/2020
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() { | |
var person = new Person { | |
FirstName = "Konstantinos", | |
LastName = "Grevenits" | |
}; | |
Console.WriteLine($ "{person.FirstName} {person.LastName}"); | |
Console.WriteLine(person.Uppercase()); | |
Console.WriteLine(person.Lowercase()); | |
} | |
public record Person { | |
public string ? FirstName { | |
get; | |
init; | |
} | |
public string ? LastName { | |
get; | |
init; | |
} | |
} | |
public static class RecordExtensions { | |
public static string Uppercase(this Person person) { | |
return $ "{person.FirstName} {person.LastName}".ToUpper(); | |
} | |
public static string Lowercase(this Person person) { | |
return $ "{person.FirstName} {person.LastName}".ToLower(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text; | |
public enum Season { | |
Spring, | |
Summer, | |
Autumn, | |
Winter | |
} | |
namespace Seasons { | |
class Program { | |
static void Main(string[] args) { | |
Console.OutputEncoding = System.Text.Encoding.UTF8; | |
Console.Write("\xfeff"); // bom = byte order mark | |
Console.WriteLine("Spring"); | |
Console.WriteLine(Season.Spring.SeasonEmoji()); | |
Console.WriteLine("Summer"); | |
Console.WriteLine(Season.Summer.SeasonEmoji()); | |
Console.WriteLine("Winter"); | |
Console.WriteLine(Season.Winter.SeasonEmoji()); | |
Console.WriteLine("Autumn"); | |
Console.WriteLine(Season.Autumn.SeasonEmoji()); | |
} | |
} | |
public static class SeasonExtensions { | |
public static string SeasonEmoji(this Season season) { | |
var emoji = ""; | |
switch (season) { | |
case Season.Spring: | |
{ | |
emoji = "🌳"; | |
break; | |
} | |
case Season.Summer: | |
{ | |
emoji = "☀️"; | |
break; | |
} | |
case Season.Winter: | |
{ | |
emoji = "❄️"; | |
break; | |
} | |
case Season.Autumn: | |
{ | |
emoji = "🍂"; | |
break; | |
} | |
} | |
return emoji; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() { | |
var myFile = new MyFile(); | |
var data = new byte[0]; //or whatever | |
myFile.ToDisk(data, "_YOUR_PATH_"); | |
myFile.ToMemory(data); | |
} | |
public class MyFile: IFile { | |
public void Save(byte[] fileData, Stream stream) { | |
//whatever | |
} | |
} | |
public interface IFile { | |
void Save(byte[] data, Stream stream); | |
} | |
public static class IFileExtensions { | |
public static FileInfo ToDisk(this IFile file, byte[] data, string filePath) { | |
using(var stream = File.Create(filePath)) { | |
file.Save(data, stream); | |
return new FileInfo(filePath); | |
} | |
} | |
public static byte[] ToMemory(this IFile file, byte[] data) { | |
using(var stream = new MemoryStream()) { | |
file.Save(data, stream); | |
return stream.ToArray(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment