2

I define 3 delegate functions in a class like

public func<datetime,string>A=>str1 { }
public func<datetime,string>B=>str2 { }
public func<datetime,string>C=>str3
{
    return a+b; 
}

how can I call a,b in c function, because return in function c is wrong?

5
  • 3
    Your code makes no sense. Try providing a FULL and CLEAR explanation of the problem. Describe EXACTLY what you expect to happen and EXACTLY what does happen. Not some vague description that requires us to work out the intention from code that doesn't achieve it. Commented May 4, 2017 at 5:15
  • Does this even compile? Commented May 4, 2017 at 5:25
  • @jmcilhinney myproblem is just I do not know how to call a delegate function. it does not need extra code, because in my description I use its functions name and its parameters type Commented May 4, 2017 at 5:28
  • @HosseinNarimaniRad no it does not compile when I call A and B function just by their names in C function as I write "return A+B" shows me a red line under that as you know means it is not correct Commented May 4, 2017 at 5:32
  • 2
    You could start by making it real code rather than pseudo-code - what is str1? What is str2? Are you intending A and B to be properties, or something else? Why have you not bothered with proper capitalization? What does this have to do with ASP.NET? (You're not using anything ASP.NET-specific in the code...) Please put more time into explaining your question clearly. Commented May 4, 2017 at 5:40

1 Answer 1

1

See that your provided code does not compile in for many reasons. To call the A and B methods you must properly call it with passing the parameters such like:

public static Func<DateTime, string> A = (date) => { return date.ToString(); };
public static Func<DateTime, string> B = (date) => { return date.ToString(); };
public static Func<DateTime, string> C = (date) => { return A(date) + B(date); };

static void Main(string[] args)
{
    var date = DateTime.Now;
    Console.WriteLine(C(date));
}

See that I also added static because without that you get the error:

A field initializer cannot reference the non-static field, method, or property Program.A

Sign up to request clarification or add additional context in comments.

3 Comments

Although I'd probably make them properties rather than public fields... (And then edit your answer to make it clear that they're fields, not methods...)
@JonSkeet - can you please elaborate so I can correct my answer?
Elaborate on which part? You're currently defining fields - I'd suggest making them public read-only properties instead, e.g. public static Func<DateTime, string> A { get; } = date => date.ToString(); etc. Then refer to A and B as delegates rather than methods, because they're not methods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.