Search This Blog

Monday, May 18, 2015

C# unit test using Moq verify property has been set

C# > Unit test > Moq > Verify property has been set

SetupSet : Specifies a setup on the mocked type for a call to a property setter.
VerifySet : Verifies that a property has been set on the mock, regardless of its value.

Example

using System;

namespace UnitTestProject1
{
    public interface Interface1
    {
        int Id { get; set; }
    }
}

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            var mocker = new Mock<Interface1>();
            mocker.SetupSet(x => x.Id = 1);
            mocker.Object.Id = 1;
            mocker.VerifySet(x => x.Id);
        }
    }
}





Friday, May 15, 2015

Convert string to date C# example

C# > System Namespace > DateTime > Parse

Parse converts string to a date.


Example


string value = "5/15/2015";

var dte = DateTime.Parse(value, CultureInfo.InvariantCulture); // {5/15/2015 12:00:00 AM}




Thursday, May 14, 2015

Find last row Telerik RadGridView

Telerik > RadGridView > Find last row
GridViewRowInfo lastRow = radGridView.Rows[radGridView.Rows.Count - 1];
lastRow.IsSelected = true;






Wednesday, May 13, 2015

C# Generic Interface Example

C# > Generics > Generic Interface 

Example


class Serializer1 { public string Name { get; set; } };
class Serializer2 { public string Name { get; set; } };

interface GenericSerializer<T>
{
      string Name(T tValue);
}

class Serializer<T>: GenericSerializer<T>
{
public string Name(T tValue)
        {
            var prop = tValue.GetType().GetProperty("Name");
            return prop.GetValue(tValue, null) as string;
        }
}

Serializer1 s1 = new Serializer1() { Name = "Serializer1" };
Serializer2 s2 = new Serializer2() { Name = "Serializer2" };

Serializer<Serializer1> serializer1 = new Serializer<Serializer1>();
string name1 = serializer1.Name(s1);

Serializer<Serializer2> serializer2 = new Serializer<Serializer2>();

string name2 = serializer2.Name(s2);






Thursday, April 23, 2015

LINQ Average excluding or including zeros c#

C# > LINQ > Average


Average computes the average of a sequence of numeric values.

Example


int[] int_array = new int[] { 5, 4, 2, 8, 10, 0 };

// linq average excluding zeros

double avg =
       (from ord in int_array
        where ord > 0
        select ord).Average();  // 5.8

// linq average including zeros 
avg =
       (from ord in int_array
       select ord).Average();    // 4.83





Wednesday, April 8, 2015

UPDATE SQL Server Example

SQL Server > DML > UPDATE

Changes existing data in a table or view.


Example


update
   person
set
   name = 'Bill'
where 
   id = 1



Wednesday, April 1, 2015

Reflection to find all classes implementing interface c#

C# > Reflection Find classes implementing interface

var iTtype = typeof(Interface);
var types = AppDomain.CurrentDomain.GetAssemblies()
           .SelectMany(a => a.GetTypes())
           .Where(t => iTtype.IsAssignableFrom(t));