// FrozenDictionaryType.cs
using System.Collections.Frozen;
namespace DotNet8Features
{
internal class FrozenDictionaryType
{
private static readonly FrozenDictionary<string, string> staticData =
LoadStaticData().ToFrozenDictionary();
private static Dictionary<string,string> LoadStaticData()
{
return new Dictionary<string, string>() { { "Key1","Value1" }, { "Key2", "Value2" }, { "Key3", "Value3" } };
}
internal string GetValue(string key)
{
if (staticData.TryGetValue(key, out string? keyValue))
{
return keyValue;
}
return string.Empty;
}
}
}
// JsonInterfaceSerializer.cs
namespace DotNet8Features
{
public interface ICar
{
public string? Brand { get; set; }
}
public interface ISedan : ICar
{
public string? Model { get; set; }
}
public class CarDetails : ISedan
{
public string? Brand { get; set; }
public string? Model { get; set; }
}
}
// Program.cs
using DotNet8Features;
using System.Text.Json;
// Feature 1: Interface serialization
ISedan detail = new CarDetails { Brand = "Honda", Model="Civic" };
var serializedValue = JsonSerializer.Serialize(detail);
Console.WriteLine(serializedValue);
// Feature 2: Frozen dictionaries
FrozenDictionaryType frozenDictionaryType = new();
Console.WriteLine($"The value returned from the frozen dictionary is {frozenDictionaryType.GetValue("Key2")}");
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)