• Code
  • Tags
  • Users
  • Titles
  • Log in
  • Feedback
  • FAQ
Share Code
Welcome to ForkCan.com

ForkCan is all about sharing code in a social way.

Discuss, debate or argue with other devs about their or your own code.

Give other devs feedback or make a Fork (Make a better version of a shared code).

Rate the code, if you use the code mark it as used so others can see if the shared code is used by someone.

Help each other to be better devs and to be more productive.


Features not working yet:

Flag a post


QR Code

Tiny Url

http://4kcan.com/s/Mjc3

Related Code
Generic Entity Framework 4.0 Base Repository
ForEach Extension method for IEnumerable<T>
Invoke Extension Method
Use of Extension methods to hide "infrastructure code"
Unity Service Locator for ASP.NET MVC 3.0 Beta 1
Generic Entity Framework 4.0 Base Repository with Paging
Generic linq-to-sql repository
WCF RIA Services Unity 2.0 DomainService Factory
A Default Entity Framework 4.0 ObjectContext Factory
UnitOfWork Action filter for ASP.NET MVC and nHibernate
Make sure the web.config pages/namespace can be used together with ASP.NET MVC 3 P1 and Razor
Email validation with Regular Expression
Reflection dynamically get private fields or properties
Custom C# exception class
Url Validation with Regular Expression
Method to hash passwords
Number of sealed / unsealed types in the framework
Extension to the HttpClient

Null Dot "Operator" Extension Method

The _ extension method will return null if the object is null, or it will return the property/method value.

2
790 0 1 0 0 1

public static class NullDotExtension
{
    public static T _<O, T>(this O o, Func<O, T> t) where T : class
    {
        return (o == null ? (T)null : t(o));
    }
}

public class NullDotObject
{
    public string StringProperty { get; set; }
    public NullDotObject ChildProperty { get; set; }
}

[TestClass]
public class NullDotTest
{
    [TestMethod]
    public void NullDotReturnsNull()
    {
        NullDotObject nullObject = null;
        Assert.IsNull(nullObject._(n => n.StringProperty));
    }

    [TestMethod]
    public void NullDotReturnsStringProperty()
    {
        NullDotObject nonNullObject = new NullDotObject { StringProperty = "String Value" };
        Assert.AreEqual<string>("String Value", nonNullObject._(n => n.StringProperty));
    }

    [TestMethod]
    public void NullDotReturnsChildProperty()
    {
        NullDotObject foo = new NullDotObject { StringProperty = "Foo" };
        NullDotObject bar = new NullDotObject { ChildProperty = foo };

        Assert.AreSame(foo, bar._(b => b.ChildProperty));
        Assert.AreEqual(foo.StringProperty, bar._(b => b.ChildProperty)._(c => c.StringProperty));
    }

    [TestMethod]
    public void NullDotReturnsNullString()
    {
        string foo = null;
        Assert.IsNull(foo._(f => f.ToLower()));
    }

    [TestMethod]
    public void NullDotReturnsString()
    {
        string foo = "FOO";
        Assert.AreEqual("foo", foo._(f => f.ToLower()));
    }
}

Share: twitter | facebook   Action: used | fork | flag

.net

Mark '.net' tag as 'like'

Mark '.net' tag as 'ignore'

c#

Mark 'c#' tag as 'like'

Mark 'c#' tag as 'ignore'


 @jeffhandley
117
September 25, 2010 9:22 AM
edited September 25, 2010 9:25 AM

Fork

 Null Dot "Operator" Extension Method -  @jeffhandley Saturday 25, 2010 9:22 AM


1 Feedback

I liked the idea of returning null or the correct value instead of getting an null exception :) -  @fredrikn Saturday 25, 2010 9:53 AM

You must log in before you can give any feedback


1 Discussion(s)

Newest Oldest
0

I am now trying to work up an approach more like this:

    [TestMethod]
    public void ImmediateNull()
    {
        TestObject test = null;
        string value = Safe<string>.Navigation(() => test.StringProperty); 
        Assert.IsNull(value);
    }

    [TestMethod]
    public void NullProperty()
    {
        TestObject test = new TestObject { StringProperty = null };
        string value = Safe<string>.Navigation(() => test.StringProperty);
        Assert.IsNull(value);
    }

    [TestMethod]
    public void NonNullProperty()
    {
        TestObject test = new TestObject { StringProperty = "foo" };
        string value = Safe<string>.Navigation(() => test.StringProperty);
        Assert.AreEqual("foo", value);
    }
link | flag  | Reply

 @jeffhandley
117
Thursday 30, 2010 7:46 AM


You must log in before you can post a comment

Squeed
Made by: Fredrik Normén 2010