C#


10
Feb 11

What if…

What if you we’re able to construct complex HTML fragments in C# with the same ease and readability as you are, using jQuery?

Stuff like…


var list = new cQuery
(
    tag: "ul",
    id: "tags",
    controls: new []
    {
        new cQuery(tag: "li", text: "C#"),
        new cQuery(tag: "li", text: "JavaScript"),

        new cQuery(tag: "li", text: "Markup languages").Append
        (
            new cQuery("ul").Append(new []
            {
                new cQuery("li", text: "XHTML"),
                new cQuery("li", text: "HTML 4"),
                new cQuery("li", text: "HTML 5")
            })
        ),

        new cQuery(tag: "li", text: "CSS")
    }
);

list.Wrap
(
    new cQuery("div").AddClass("horizontal").Append
    (
        new cQuery(new LinkButton { ID = "hidelist", Text = "Hide" }).AddClass("small")
    )
);

PlaceHolder.Controls.Add(list);

I will be releasing my cQuery class shortly. Stay tuned.


10
Feb 11

Syntatic sugar: a different way to do if-statements

Try this one out for size.


var value = "no";

if ({ foo: true, bar: true, yes: true, maybe: true }[value]) // will return false

var value = "bar";

if ({ foo: true, bar: true, yes: true, maybe: true }[value]) // will return true

var validValues = {
    foo: true,
    bar: true,
    yes: true,
    maybe: true
};

if (validValues["bar"]) // will return true

C# you say?


if (new [] { "foo", "bar", "yes", "maybe" }.Contains("bar")) // returns true

var validValues = new []
{
    "foo",
    "bar",
    "yes",
    "maybe"
};

if (validValues.Contains("bar")) // returns true