C
C#3y ago
zxa4fd

How does this delegate work?

Given this delegate:
public delegate (T Value, int Seed) Generator<T>(int seed)
public delegate (T Value, int Seed) Generator<T>(int seed)
How this function return a Generator delegate?
public static Generator<int> NextInt = (seed) =>
{
seed ^= seed >> 13;
seed ^= seed << 18;
int result = seed & 0x7fffffff;
return (result, result);
};
public static Generator<int> NextInt = (seed) =>
{
seed ^= seed >> 13;
seed ^= seed << 18;
int result = seed & 0x7fffffff;
return (result, result);
};
8 Replies
Thinker
Thinker3y ago
Well, NextInt is a static field with the type Generator<int>, and it's initialized with a lambda expression which matches the Generator<int> delegate.
zxa4fd
zxa4fdOP3y ago
This seems correct; I thought NextInt was a function and not a field
Thinker
Thinker3y ago
It doesn't have () so it's not a method
zxa4fd
zxa4fdOP3y ago
This NextInt property is used here in what seems to be LINQ:
public static Generator<bool> NextBool =>
from i in NextInt
select i % 2 == 0;
public static Generator<bool> NextBool =>
from i in NextInt
select i % 2 == 0;
May I know how this is working? NextInt doesn't seem to be IEnumerable
Thinker
Thinker3y ago
That shouldn't work
zxa4fd
zxa4fdOP3y ago
This is an example from a book, so maybe the context will help? Will it really not work?
Thinker
Thinker3y ago
yeah no this doesn't make sense

Did you find this page helpful?