Friday, January 26, 2018

C# - Implement Generic IEnumerable in Simple Example

class MyList<T> : IEnumerable<T>
{
    T[] m_Items = null;
    int freeIndex = 0;

    public MyList()
    {
        // For the sake of simplicity lets keep them as arrays
        // ideally it should be link list
        m_Items = new T[100];
    }

    public void Add(T item)
    {
        // Let us only worry about adding the item 
        m_Items[freeIndex] = item;
        freeIndex++;
    }

    #region IEnumerable<T> Members

    public IEnumerator<T> GetEnumerator()
    {
        foreach (T t in m_Items)
        {
            // Lets check for end of list (its bad code since we used arrays)
            if (t == null// this wont work is T is not a nullable type
            {
                break;
            }

            // Return the current element and then on next function call 
            // resume from next element rather than starting all over again;
            yield return t;
        }
    }

    #endregion

    #region IEnumerable Members

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        // Lets call the generic version here
        return this.GetEnumerator();
    }

    #endregion
}

Use it:

MyList<string> myListOfStrings = new MyList<string>();

    myListOfStrings.Add("one");
    myListOfStrings.Add("two");
    myListOfStrings.Add("three");
    myListOfStrings.Add("four");

    foreach (string s in myListOfStrings)
    {
        Console.WriteLine(s);
    }

Wednesday, January 24, 2018

C# - Return and Yield differences

static int SimpleReturn()
{
    return 1;
    return 2;
    return 3;
}

static void Main(string[] args)
{
    // Lets see how simeple return works
    Console.WriteLine(SimpleReturn());
    Console.WriteLine(SimpleReturn());
    Console.WriteLine(SimpleReturn());
    Console.WriteLine(SimpleReturn());
}

The reason for this is that the normal return statement does not preserve the state of the function while returning. i.e. every call to this function is a new call and it will return the first value only.

-----------------------------------------------------------------------------------------------------------------------

Where as if I replace the return keyword by yield return then the function will become capable of saving its state while returning the value. i.e. when the function is called second time, it will continue the processing from where is has returned in the previous call.

static IEnumerable<int> YieldReturn()
{
    yield return 1;
    yield return 2;
    yield return 3;
}
static void Main(string[] args)
{
    // Lets see how yield return works
    foreach (int i in YieldReturn())
    {
        Console.WriteLine(i);
    }
}

Friday, January 19, 2018

Razor - Verbatim String in Razor Example

@(SiteSettings.SettingsPage.Debug == true ? @"data-debug=""true""" :  @"data-debug=""false""")

Tuesday, January 16, 2018

Javascript, jQuery - Toggle Between Two Functions

let Partner = {
init() {
const $ = require('jquery');

var $selector = 11;
var $link = document.querySelector('[data-clickme]');
var $list = $('.partnerblock__grid a');
var $listSize = $('.partnerblock__grid a:visible').length;

hideListVisibility($, $list, $selector);

$($link).on('click', function(e) {
e.preventDefault();

if ($(this).attr('data-click-state') == 1) {

$(this).attr('data-click-state', 0)
hideListVisibility($, $list, 11);

} else {
$(this).attr('data-click-state', 1)
showListVisibility($, $list, 48);
}

})

}

};

function hideListVisibility($, $list, $selector) {
$($list).each(function() {
var $elem = $(this);
if ($elem.index() > $selector) {
$elem.addClass("hide");
};
});
}

function showListVisibility($, $list, $selector) {
$($list).each(function() {
var $elem = $(this);
if ($elem.index() < $selector) {
$elem.removeClass("hide");
};
});
}

export default Partner;