-3

I am trying to implement one interface and it has such method

public object GetOrCreate<T>(string key, Func<ICacheEntry, T> func)
{

}

From the method declaration, I find that ICacheEntry and generic T type object is passed. Maybe someone could help to understand how to access these two parameters in the method?

Tomas
  • 17,551
  • 43
  • 152
  • 257

1 Answers1

0

Like this:

public object GetOrCreate<T>(string key, Func<ICacheEntry, T> func)
{
   ICacheEntry someCacheEntry = ...;

   T someTinstance = func(someCacheEntry);

   return (object)someTinstance;
}

func is a delegate method parameter.

https://www.tutorialsteacher.com/csharp/csharp-func-delegate

T is a generic type parameter, it is not really a type, it is a passe-partout substitutable parameter for unknown type. T comes from the word template.

You may want to read these tutorials:

C# Generics Level 1

C# Generics Level 2

Generics in .NET

To learn C#:

C# Tutorial Level 0

C# Tutorial Level 1

C# Tutorial Level 2

C# Tutorial Level 3

C# Snippets @ Techi.io

Beginning Visual C# 2008 Programming (Book)

  • 2
    There is no way anyone here can decisively tell you what to do with the key parameter, you need to consult the documentation or use of this method to figure out what the key should be used for. I can guess though, that this is some sort of dictionary method, in which case the key is used to distinguish the caching of several distinct values by a key. – Lasse V. Karlsen Oct 03 '19 at 15:54
  • @OlivierRogier Not sure why your answer downvoted but it works and explains a lot. Thank you! – Tomas Oct 03 '19 at 16:17
  • Idea. Perhaps key is to use to get a ICacheEntry like this: `ICacheEntry someCacheEntry = someProvider(key, ...)` –  Oct 03 '19 at 16:21