Tuesday, July 21, 2015

C# Notation Reminder

I often forget the nomenclature of a few C# coding constructs. Maybe writing them down will help.

In C# 2.0, generics were introduced. For a generic there is a <T> which stands for Generic Type Parameter. The generic type is a blueprint for what you actually want to implement. An example is the code below where I want to implement an IoC container to write session details to Redis.


            /// <summary>
            /// funq IoC container for in memory cache client like Redis
            /// used for Auth
            /// </summary>
            /// <param name="container"></param>
            public override void Configure(Container container)
            {
                Plugins.Add(new AuthFeature(
                    () => new AuthUserSession(),
                    new IAuthProvider[] {new BasicAuthProvider(), }));

                container.Register<ICacheClient>(new MemoryCacheClient());
                var userRepository = new InMemoryAuthRepository();
                container.Register<IUserAuthRepository>(userRepository);
            }
        }

The other thing I forget is what () => means in for lambdas. To create a lambda expression, you optionally specify input parameters on the left side of the lambda operator =>, and you put the expression or statement block on the other side. I always forget that () is just an empty parameter list. I am not even sure why I used an empty list in the delegate above. Why!?

For example, the lambda expression x => x * x specifies a parameter that’s named x and returns the value of x squared.

REFS:
https://msdn.microsoft.com/en-us/library/0zk36dx2.aspx
https://msdn.microsoft.com/en-us/library/bb397687.aspx

1 comment: