CsvMappingItem(Int32) Property

Gets the DynamicProperty with the specified index.

Definition

Namespace: FolkerKinzel.CsvTools.Mappings
Assembly: FolkerKinzel.CsvTools.Mappings (in FolkerKinzel.CsvTools.Mappings.dll) Version: 1.1.0+1263e8243dc2cd78095f678f813d7d9c52ea4315
C#
public DynamicProperty this[
	int index
] { get; }

Parameters

index  Int32
Zero-based index of the DynamicProperty instance.

Property Value

DynamicProperty

Remarks

The index corresponds to the order in which the DynamicProperty instances had been added to the CsvMapping.

Example

Object serialization with CSV:

C#
using FolkerKinzel.CsvTools;
using System.Globalization;

namespace Benchmarks;

internal static partial class CalculationWriter
{
    // This example completely dispenses with CsvMapping and uses CsvWriter
    // directly. (This approach is only recommended for writing CSV.)
    internal static string WritePerformance(Calculation[] data)
    {
        CultureInfo culture = CultureInfo.InvariantCulture;
        const string doubleFormat = "G17";

        using var stringWriter = new StringWriter();
        using var csvWriter = new CsvWriter(stringWriter, ["First", "Operator", "Second", "Result"]);

        ReadOnlySpan<Calculation> dataSpan = data;
        Span<ReadOnlyMemory<char>> recordSpan = csvWriter.Record.Values;

        for (int i = 0; i < dataSpan.Length; i++)
        {
            Calculation calculation = dataSpan[i];

            recordSpan[0] = calculation.First.ToString(doubleFormat, culture).AsMemory();
            recordSpan[1] = GetOperator(calculation.Operator).AsMemory();
            recordSpan[2] = calculation.Second.ToString(doubleFormat, culture).AsMemory();
            recordSpan[3] = calculation.Result.ToString(doubleFormat, culture).AsMemory();

            csvWriter.WriteRecord();
        }

        return stringWriter.ToString();

        static string GetOperator(char op)
        {
            return op switch
            {
                '+' => "+",
                '-' => "-",
                '*' => "*",
                '/' => "/",
                '%' => "%",
                _ => "",
            };
        }
    }
}

Exceptions

ArgumentOutOfRangeExceptionindex is less than zero or greater than or equal to Count.

See Also