How can I change the signature of my Action delegates to NOT use void methods in my Dictionary<bool, Action<UrlParts, List<UrlParts>>>?

public static Func<UrlParts, List<UrlParts>, bool> LacksDomainPage = (up, lup) => !lup.Contains(up);

public static Action<UrlParts, List<UrlParts>> LacksUrl = (up, lup) => dicAddDomainPage[LacksDomainPage(up, lup)](up, lup);

public static Dictionary<bool, Action<UrlParts, List<UrlParts>>> dicAddDomainPage
                = new Dictionary<bool, Action<UrlParts, List<UrlParts>>>() { { true, AddDomainPage }, { false, AddNothing }, };

using

public static Action<UrlParts, List<UrlParts>> AddDomainPage = (up, ldp) => ldp.Add(up);
public static Action<UrlParts, List<UrlParts>> AddNothing = (up, ldp) => { };

instead of

public static void AddDomainPage(UrlParts up, List<UrlParts> ldp)
{
    ldp.Add(up);
}

public static void AddNothing(UrlParts up, List<UrlParts> ldp) { }

Could you not use Func delegates instead of Action delegates?

Thanks for your reply.

Since the program isn't returning anything, I am unsure how I would use Func. Could you elaborate?

Here is another example of what I am asking.

        public static Dictionary<string, Action> Testing = new Dictionary<string, Action>() 
        { 
            { "one", TestOne }, 
            { "two", TestTwo }, 
        };

        static void Main(string[] args)
        {
            Testing["one"]();
        }

        public static Action TestOne = () => { Debug.WriteLine("TestOne"); };
        public static Action TestTwo = () => { Debug.WriteLine("TestTwo"); };

Try this:

class Program
    {   
        static void Main(string[] args)
        {
            Dictionary<string, Action> Testing = new Dictionary<string, Action>();
            Testing["one"] = new Action(TestOne); 
            Testing["two"] = new Action(TestTwo); 
            Testing["one"].Invoke();
            Testing["two"].Invoke();
            Console.ReadKey();
        }

        public static Action TestOne = () => { Console.WriteLine("TestOne"); };

        public static Action TestTwo = () => { Console.WriteLine("TestTwo"); };       
    }

I've been looking at the problem and stumbled my way to:

private static Dictionary<string, Action> dsa1 { get { return new Dictionary<string, Action>() { { "one", dsa1TestOne } }; } }
private static Dictionary<string, Action> dsa2 { get { return new Dictionary<string, Action>() { { "one", dsa2TestOne } }; } }

private static Dictionary<string, Action> dsa3 = new Dictionary<string, Action>()  { { "one", dsa1TestOne } };
private static Dictionary<string, Action> dsa4 = new Dictionary<string, Action>() { { "one", dsa2TestOne } };        

static void Main(string[] args)
{
    Dictionary<string, Action> dsa3_again = new Dictionary<string, Action>()  { { "one", dsa1TestOne } };
    Dictionary<string, Action> dsa4_again = new Dictionary<string, Action>() { { "one", dsa2TestOne } };        

    dsa1["one"]();
    dsa2["one"]();
    dsa3["one"]();
    dsa4["one"]();
    dsa3_again["one"]();
    dsa4_again["one"]();
}

public static Action dsa1TestOne = () => { Debug.WriteLine("TestOne"); };

public static void dsa2TestOne()
{
    Debug.WriteLine("TestOne");
}

Why would a Dictionary of string to Action's collection initializer require a void method at class scope?

Testing the above showed that dsa3 will throw a NullReferenceException on the Action delegate and not the void.

You not only have to instantiate the dictionary, but also the actions you put in it. See my example.

That is at the heart of the question. The collection initializer (for Generics or just the Dictionary?) only accepts void at runtime when Action is specified.

For the constructor at the class scope. My fourth post help me discover that the properties worked fine as well as the class level Action to void constructor and the program scope constructors.

I am just curious if that is by design or something else.

An Action delegate ALWAYS returns void. Use Func if you don't want that to be the case. See here.

I am failing to see how I have NOT clearly demonstrated I know what an Action delegate is. Or how ANY of my POSTED code requires the need for a Func delegate.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.