C
Join ServerC#
help
Any way to create another instance of a List<Tuple> with the same setter?
Ooe1191/25/2023
I have this within an abstract class:
Is there any way to simplify it?
I am basically writing the exact same logic for 2 tuples, just with different names.
Thanks!
private List<Tuple<string, string>> _Headers;
public List<Tuple<string, string>> Headers
{
get { return this._Headers; }
set
{
foreach (Tuple<string, string> kvp in value)
this._Headers.Add(new Tuple<string, string>(kvp.Item1.Replace("\"", "\\\"")
, kvp.Item2.Replace("\"", "\\\"")));
}
}
private List<Tuple<string, string>> _Cookies;
public List<Tuple<string, string>> Cookies
{
get { return this._Cookies; }
set
{
foreach (Tuple<string, string> kvp in value)
this._Cookies.Add(new Tuple<string, string>(kvp.Item1.Replace("\"", "\\\"")
, kvp.Item2.Replace("\"", "\\\"")));
}
}
Is there any way to simplify it?
I am basically writing the exact same logic for 2 tuples, just with different names.
Thanks!
Tthinker2271/25/2023
Firstly, use
(string, string)
(ValueTuple<string, string>
) instead of Tuple<string, string>
Ooe1191/25/2023
I'm fine with Tuple<string, string> honestly
Ooe1191/25/2023
The frontend reads it fine and its not that ambiguous
Ooe1191/25/2023
Anyone can deduct what header.Item1 & header.Item2 are
EEro1/25/2023
It's not about ambiguity or anything like that
EEro1/25/2023
It's more efficient, less allocations, not a reference type, can be equated
Ooe1191/25/2023
What does equated mean? @Ero
Ooe1191/25/2023
I don't see my Tuple being inneficient for my use case though
Aarion1/25/2023
A major problem with the tuple class is that it is a reference-type with memory allocation on the heap. Due to allocations and garbage collection pressure, using it can lead to CPU-intensive operations and major performance issues in our system. The ValueTuple on the other hand is a lightweight value type object and has its memory stored on the stack. This means that with ValueTuples we can build better-optimized systems.- https://code-maze.com/csharp-valuetuple-vs-tuple/
Ooe1191/25/2023
Ok
Ooe1191/25/2023
I'll change to ValueTuple
Ooe1191/25/2023
How could I fix the problem I'm facing in the title of this thread though?
EEro1/25/2023
You could select on it
Ooe1191/25/2023
private List<ValueTuple<string, string>> _HeaderVT;
public List<ValueTuple<string, string>> HeaderVT
{
get
{
return _HeaderVT;
}
set
{
foreach (var kv in _HeaderVT)
this._Headers.Add(new Tuple<string, string>(kv.Item1.Replace("\"", "\\\"")
, kv.Item2.Replace("\"", "\\\"")));
}
}
Ooe1191/25/2023
now wot
Ooe1191/25/2023

EEro1/25/2023
Don't use literally
ValueTuple
EEro1/25/2023
Thinker showed you how to do it
Aarion1/25/2023
(string, string)
EEro1/25/2023
List<(string, string)>
Ooe1191/25/2023
private List<(string, string)> _HeaderVT;
public List<(string, string)> HeaderVT
Ooe1191/25/2023
ok
Ooe1191/25/2023
but how do i create like a "reusable type" out of that like the title asks
Ooe1191/25/2023
with the same setter
EEro1/25/2023
What do you mean?
EEro1/25/2023
"reusable type"?
Ooe1191/25/2023
that was probably the wrong way to describe it
Ooe1191/25/2023
but
Ooe1191/25/2023
ill show you
Ooe1191/25/2023
private List<Tuple<string, string>> _Headers;
public List<Tuple<string, string>> Headers
{
get { return this._Headers; }
set
{
foreach (Tuple<string, string> kvp in value)
this._Headers.Add(new Tuple<string, string>(kvp.Item1.Replace("\"", "\\\"")
, kvp.Item2.Replace("\"", "\\\"")));
}
}
private List<Tuple<string, string>> _Cookies;
public List<Tuple<string, string>> Cookies
{
get { return this._Cookies; }
set
{
foreach (Tuple<string, string> kvp in value)
this._Cookies.Add(new Tuple<string, string>(kvp.Item1.Replace("\"", "\\\"")
, kvp.Item2.Replace("\"", "\\\"")));
}
}
Ooe1191/25/2023
imagine this used a valuetuple
Ooe1191/25/2023
ignore the tuple<string>
Ooe1191/25/2023
how would I prevent boilerplate here
Ooe1191/25/2023
im technically writing the same setter for the two instances
Ooe1191/25/2023
surely theres a way to make this shorter and cleaner?
EEro1/25/2023
(string, string) MakeLiteral((string, string) tuple)
{
return (tuple.Item1.Replace(...), tuple.Item2.Replace(...));
}
//
set => _cookies = value.Select(MakeLiteral).ToList();
Ooe1191/25/2023
is there not a way to create like a "custom" ValueTuple that contains the getters and the setters already?
Ooe1191/25/2023
seems like im still repeating some code here no? @Ero
Ooe1191/25/2023
(string, string) MakeLiteral((string, string) tuple)
{
return (tuple.Item1.Replace("\"", "\\\""), tuple.Item2.Replace("\"", "\\\""));
}
private List<(string, string)> _Header;
public List<(string, string)> Header
{
get => _Header;
set => _Header = value.Select(MakeLiteral).ToList();
}
private List<(string, string)> _Cookies;
public List<(string, string)> Cookies
{
get => _Cookies;
set => _Cookies = value.Select(MakeLiteral).ToList();
}
Ooe1191/25/2023
to be fair, its pretty short now, but just for learning purposes is there any way to do this?
AAntonC1/25/2023
make a record struct for that
Ooe1191/25/2023
and how would I override the .Add method? I just realised that i can't use getters and setters because its a collection and im adding individual elements
AAntonC1/25/2023
you're overcomplicating it
Ooe1191/25/2023
the problem is this doesn't work @AntonC
AAntonC1/25/2023
also setters should be idempotent
Ooe1191/25/2023
because setting is not the same as adding
AAntonC1/25/2023
do AddRange then
AAntonC1/25/2023
but adding elements in a setter is just wrong
AAntonC1/25/2023
it should be idempotent
Ooe1191/25/2023
idempotent 

Ooe1191/25/2023
public (string, string) MakeLiteral((string, string) tuple)
{
return (tuple.Item1.Replace("\"", "\\\""), tuple.Item2.Replace("\"", "\\\""));
}
public List<(string, string)> Headers = new List<(string, string)>();
public List<(string, string)> Cookies = new List<(string, string)>();
Ooe1191/25/2023
i simplified it
Ooe1191/25/2023
foreach (var cookie in harEntry.Request.Cookies)
Cookies.Add(MakeLiteral((cookie.Name, cookie.Value)));
Ooe1191/25/2023
as you said, i am complicating it
Ooe1191/25/2023
this works
AAntonC1/25/2023
make a method
AddCookies
that takes an IEnumerable
of those tuplesAAntonC1/25/2023
it means applying the same operation does nothing after each consecutive application
AAntonC1/25/2023
make a named record struct instead of that tuple
AAntonC1/25/2023
use the implicit
new()
for the field initialization tooOoe1191/25/2023
ok