C
C#2y ago
oe

Any way to create another instance of a List<Tuple> with the same setter?

I have this within an abstract class:
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("\"", "\\\"")));
}
}
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!
26 Replies
Thinker
Thinker2y ago
Firstly, use (string, string) (ValueTuple<string, string>) instead of Tuple<string, string>
oe
oeOP2y ago
I'm fine with Tuple<string, string> honestly The frontend reads it fine and its not that ambiguous Anyone can deduct what header.Item1 & header.Item2 are
ero
ero2y ago
It's not about ambiguity or anything like that It's more efficient, less allocations, not a reference type, can be equated
oe
oeOP2y ago
What does equated mean? @Ero I don't see my Tuple being inneficient for my use case though
arion
arion2y ago
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/
oe
oeOP2y ago
Ok I'll change to ValueTuple How could I fix the problem I'm facing in the title of this thread though?
ero
ero2y ago
You could select on it
oe
oeOP2y ago
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("\"", "\\\"")));
}
}
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("\"", "\\\"")));
}
}
now HmmRTX
ero
ero2y ago
Don't use literally ValueTuple Thinker showed you how to do it
arion
arion2y ago
(string, string)
ero
ero2y ago
List<(string, string)>
oe
oeOP2y ago
private List<(string, string)> _HeaderVT; public List<(string, string)> HeaderVT ok but how do i create like a "reusable type" out of that like the title asks with the same setter
ero
ero2y ago
What do you mean? "reusable type"?
oe
oeOP2y ago
that was probably the wrong way to describe it but ill show you
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("\"", "\\\"")));
}
}
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("\"", "\\\"")));
}
}
imagine this used a valuetuple ignore the tuple<string> how would I prevent boilerplate here im technically writing the same setter for the two instances surely theres a way to make this shorter and cleaner?
ero
ero2y ago
(string, string) MakeLiteral((string, string) tuple)
{
return (tuple.Item1.Replace(...), tuple.Item2.Replace(...));
}

//
set => _cookies = value.Select(MakeLiteral).ToList();
(string, string) MakeLiteral((string, string) tuple)
{
return (tuple.Item1.Replace(...), tuple.Item2.Replace(...));
}

//
set => _cookies = value.Select(MakeLiteral).ToList();
oe
oeOP2y ago
is there not a way to create like a "custom" ValueTuple that contains the getters and the setters already? seems like im still repeating some code here no? @Ero
(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();
}
(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();
}
to be fair, its pretty short now, but just for learning purposes is there any way to do this?
Anton
Anton2y ago
make a record struct for that
oe
oeOP2y ago
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
Anton
Anton2y ago
you're overcomplicating it
oe
oeOP2y ago
the problem is this doesn't work @AntonC
Anton
Anton2y ago
also setters should be idempotent
oe
oeOP2y ago
because setting is not the same as adding
Anton
Anton2y ago
do AddRange then but adding elements in a setter is just wrong it should be idempotent
oe
oeOP2y ago
idempotent pikawhat
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)>();
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)>();
i simplified it
foreach (var cookie in harEntry.Request.Cookies)
Cookies.Add(MakeLiteral((cookie.Name, cookie.Value)));
foreach (var cookie in harEntry.Request.Cookies)
Cookies.Add(MakeLiteral((cookie.Name, cookie.Value)));
as you said, i am complicating it this works
Anton
Anton2y ago
make a method AddCookies that takes an IEnumerable of those tuples it means applying the same operation does nothing after each consecutive application make a named record struct instead of that tuple use the implicit new() for the field initialization too
oe
oeOP2y ago
ok
Want results from more Discord servers?
Add your server