Explicit interface implementation

As they say: you live and learn. Some time ago I wrote about why I don’t like VB.NET, where I was complaining about strange VB.NET behaviour with implementing interfaces. And recently I found out that in C# there is also something called Explicit Interface Implementation.

So if we take example from my previous post:

interface IDoNothing
    {
        void Method();
    }

    class FirstClass : IDoNothing
    {
        public void Method()
        {
            Console.WriteLine("FirstClass.Method");
        }

        void IDoNothing.Method()
        {
            Console.WriteLine("FirstClass.IDoNothing.Method");
        }
    }
    class PrivateClass : IDoNothing
    {
        public void Method()
        {
            Console.WriteLine("PrivateClassClass.Method");
        }
    }

And then we can call it:

    public class Program
    {
        public static void Main()
        {
            Console.WriteLine("As Interface:");
            IDoNothing asInterface = new FirstClass();
            //Outputs: FirstClass.IDoNothing.Method
            asInterface.Method();

            Console.WriteLine("As Class:");
            FirstClass asClass = new FirstClass();
            //Outputs: FirstClass.Method
            asClass.Method();
            //Outputs: FirstClass.ThisIsMethod
            ((IDoNothing)asClass).Method();

            Console.WriteLine("As Private Class:");
            IDoNothing asPrivateClass = new PrivateClass();
            //Outputs: PrivatClass.Method
            asPrivateClass.Method();
        }
    }

It is quite similar to VB.NET, but for me it much more clearer and there are less possibilities for create obfuscated code (like defining private method which is actually public).
Czytaj dalej Explicit interface implementation

Easy way to run SQL stored procedure in C# using dynamics

I don’t like standard way of calling stored procedures in .NET with SqlCommand. So when I was starting one of projects, I thought that maybe I could use some helper that would allow me to easier way of calling procedures. As I didn’t find anything interesting, I’ve created one by myself. And this is one of this moments, when you feel why dynamics in C# are so great thing.
Now if I want to save some data using stored procedure I can use code like that:

SqlHelper.SaveData("SP_SAVE_IMPORTANT_DATA", new
{
    firstParameter= "This is value",
    allData = 42
});

To get data fro Czytaj dalej Easy way to run SQL stored procedure in C# using dynamics