Can I optimize checking for multiple null values in C# - c#

I have the following code:
if (Session["CurrentUrl"] != null & Session["CurrentHost"] != null)
I use this in many places. I am wondering if anyone can think of a way to optimize the C# code just to make things slight more clear.

This seems like a prime candidate for an extension method:
public static class SessionExtensions
{
public static bool HasHostAndUrl(this HttpSessionState session)
{
return session["CurrentUrl"] != null && session["CurrentHost"] != null;
}
}
And then:
if (Session.HasHostAndUrl()) { /* ... */ }

if (HostAndUrlExist()) {
...
}
public boolean HostAndUrlExist() {
return Session["CurrentUrl"] != null && Session["CurrentHost"] != null
}

Make a method to return true or false

If you have groups of session variables that you often use together, consider storing them as an object instead of as a set of primitive types.

You can make a method that takes one or more strings, like this:
public boolean SessionObjectsPresent(params string[] names) {
foreach (var name in names) {
if (Session[name] == null) return false
}
return true;
}
if (SessionObjectsPresent("CurrentHost", "CurrentUrl")) {
// ...
}
The benefit of this approach becomes evident when you need to test session for other things:
if (SessionObjectsPresent("CurrentUser", "LastEditTime")) {
// ...
}

Several ways to improve, like abstract a method with a meaningful name, or extract a interface ISession to make it more unit testable.

Related

C# null check chain in method call

I suppose method call chain below.
void DoSomething()
{
ObjectA a = CreateA();
if (a != null)
{
a.Foo();
}
}
ObjectA CreateA()
{
ObjectB b = CreateB();
if (b != null)
{
ObjectA a = b.ToA();
return a;
}
return null;
}
If method call depth get deeper, null checking will be more overlapped.
Is there any good solution for this?
Modified
I changed example code. It can't solve my problem that change CreateA to constructor.
The problem is only unnecessary null check chaining overlapping.
void SetImage()
{
UISprite image = GetSprite();
if (image != null)
{
image.spriteName = "hat";
}
}
UISprite GetSprite()
{
UISprite image = GetComponent<UISprite>();
if (image != null)
{
image.width = 100;
image.height = 100;
return image;
}
return null;
}
Starting with C# 6.0 you can use Null-Conditional Operator, which lets you make null-checking implicitly:
var result = possiblyNull?.MethodThatCanReturnNull()?.SomeProperty;
This construct will produce a null result if any element in the chain produces null.
You can do
void DoSomething()
{
CreateA()?.Foo();
}
ObjectA CreateA()
{
return CreateB()?.ToA();
}
Your other approach if you can't use C# 6, is don't return nulls, use null objects that way you never have to deal with null checking ( but you can still check if something is the null object )
If you are using C# 6.0 or higher, you got an easy solution with Null conditional operators for this issue.
see this link
https://msdn.microsoft.com/en-au/library/dn986595.aspx?f=255&MSPPError=-2147217396&cs-save-lang=1&cs-lang=csharp#code-snippet-1
So, assuming you (or someone else) can't use the null-conditional operator, is there a good reason to be using this pattern of methods creating objects instead of constructors creating the objects? Constructors are guaranteed not to return null.
It looks like you have some conversion or nested object heirarchy, but no inheritance heirarchy where you could just fall back on polymorphism. Maybe a tool like AutoMapper could be useful to encode these ToX() methods in a consistent manner?
I'm not sure how "nested" this would be. Your CreateB() method is going to look exactly like your CreateA() code. You're not going to end up with a "pyramid," just a lot of identical methods.
ObjectB CreateB()
{
ObjectC c = CreateC();
if (c != null)
{
ObjectB b = c.ToB();
return b;
}
return null;
}
Most of the time, you're doing this in an environment where you don't control all the classes. In that case, writing your own conversion functions or AutoMapper (really, worth the time) is the best approach. But, if you own the class hierarchy you might implement an abstract class that will do most of the heavy lifting for you. But honestly, I would only write something like this if I had a really good reason (something more than I just wanted to fuck with people). I include this to demonstrate how much simpler life is if you just use a constructor, which is guaranteed not to return null;
public abstract class MyAbstractObject<Tobj> where TObj: MyAbstractObject, new()
{
public static MyAbstractObject CreateObject()
{
Tobj subOb = new TObj();
MyAbstractObject parent = subOb.ToObject();
return parent;
}
public virtual TObj ToObject()
{
return CreateObject();
}
}
public class ObjectD : MyAbstractObject<ObjectC> { }
public class ObjectC : MyAbstractObject<ObjectB> { }
public class ObjectB : MyAbstractObject<ObjectA> { }
public class ObjectA : MyAbstractObject<ObjectA>
{
public override TObj ToObject()
{
return this;
}
}
static void Main()
{
ObjectA a = ObjectD.CreateObject();
}

Syntax for implicit operator with class indexer

I am looking for the correct syntax to use an implicit operator on a class that uses an indexer to acess a private Dictionary:
[System.Serializable]
public class MyClass : IEnumerable
{
private Dictionary<string, object> vars = new Dictionary<string, object>();
public object this[string key]
{
get
{
if(vars.ContainsKey(key))
{
return (object)vars[key];
}
else
{
return null;
}
}
set
{
object o = value;
if(!vars.ContainsKey(key))
{
vars.Add(key, o);
}
else if(value == null)
{
vars.Remove(key);
}
else
{
vars[key] = o;
}
}
}
/*some code*/
public static implicit operator bool(WorldVars w, string i)
{
if(w[i] != null)
{
return true;
}
else
{
return false;
}
}
}
Right now the use is pretty straight forward
MyClass[anykey] = myValue
but I'd like to implement a quicker way to test the presence of a value, like:
if(MyClass[anykey])
{ //logic }
As commenter Eric notes, the semantics of your class would be completely broken if you were able to achieve what you're asking for. The whole point of the indexer is so that when you write the expression myClass[anyKey], it evaluates to the value that your class associates with anyKey.
If you were to change the implementation so that it simply returned a bool value representing containment, then you'd be stuck having to implement some other mechanism to actually retrieve the value (e.g. a separate method). Additionally, it would also raise the question of what the setter should do.
Given the implementation you show, it seems to me that writing if (myClass[anyKey] != null) is not really inconvenient, and it seems reasonably expressive to me. That is, it is a reasonable way for the code to clearly express its intent.
That said, if you did want something more expressive, it would not be unreasonable to write a ContainsKey() method in your class for the purpose:
public bool ContainsKey(string key) { return vars.ContainsKey(key); }
Then you could check for the key's presence like:
if (myClass.ContainsKey[anyKey]) { ... }
Finally, the code you posted should work acceptably well, but it seems overly verbose and inconsistent to me. IMHO, a better way to write your indexer methods would be something like this:
public object this[string key]
{
get
{
object o;
return vars.TryGetValue(key, out o) ? o : null;
}
set
{
if (value != null)
{
vars[key] = value;
}
else
{
vars.Remove(key);
}
}
}
That implementation avoids things like:
Redundant check for containment when getting a value
Copying value into local variable unnecessarily when setting a value
Having two different lines of code that each both have the effect of setting the value for a key in the dictionary

Refactor if statements with different conditions

I'm trying to refactor a method consisting of multiple if-statements with different conditions.
The method looks like this:
private void DeserializeProperty(object value,
PropertyInfo property, Format format) {
if (value == DBNull.Value || value == null) {
property.SetValue(this, null);
return;
}
if (property.PropertyType == typeof(Id)) {
SetIdProperty(value, property);
return;
}
if (property.PropertyType.BaseType == typeof(BaseProperty)) {
SetBaseProperty(value, property);
return;
}
if (property.PropertyType == typeof(Data)) {
DeserializeData(value, property, format);
return;
}
DeserializeNormalProperty(property, value);
}
Replacing these If-statements with polymorphism won't work afaik (not sure if it would be wise to do it if it would work) since the condition is concerning the PropertyType
Replacing them with a Dictionary of type Dictionery<Type,Action<object, PropertyInfo>> won't work since the method
DeserializeData(value, property, format);
would not fit in the action
Additionally non of the Solutions above would handle the value == DBNull.Value || value == null Condition
How do I solve this?
Just a quick warning - neither of the below are refactors as such, just ways of rewriting what you already have. Given the variable nature of the conditions and their imposed ordering (eg basetype of baseproperty taking precedence over data), it is hard to think of anything better or clearer than an if-else. IMO you can't really improve on what you have by just refactoring that one function. But I'll present some possible re-writes anyway in case either appeals.
Could use elses (simply for readability):
Note: Under some coding guidelines, this is less preferential to what you already have. Personally, I find it more readable however.
private void DeserializeProperty(object value,
PropertyInfo property, Format format) {
if (value == DBNull.Value || value == null) {
property.SetValue(this, null);
}
else if (property.PropertyType == typeof(Id)) {
SetIdProperty(value, property);
}
else if (property.PropertyType.BaseType == typeof(BaseProperty)) {
SetBaseProperty(value, property);
}
else if (property.PropertyType == typeof(Data)) {
DeserializeData(value, property, format);
}
else {
DeserializeNormalProperty(property, value);
}
}
An Alternative (if you so desire)
If you are doing lots of similar switches, using a custom CleverSwitch class similar to this:
https://stackoverflow.com/a/299120/3940783
might work for you. It'd be rather similar to your idea about the dictionary of Actions - but due to the variation in your conditions, it'd be cleanest to have Actions with no inputs and just use the variables from the DeserializeProperty's scope without passing them into the action.
So, for example, you could replace the function with the following:
private void DeserializeProperty(object value,
PropertyInfo property, Format format) {
CleverSwitch.Do(
CleverSwitch.If(() => value == DBNull.Value || value == null, () => property.SetValue(this, null))
CleverSwitch.IsType<Id>(property.PropertyType, () => SetIdProperty(value, property)),
CleverSwitch.IsType<BaseProperty>(property.PropertyType.BaseType, () => SetBaseProperty(value, property)),
CleverSwitch.IsType<Data>(property.PropertyType, () => DeserializeData(value, property, format)),
CleverSwitch.Default(() => DeserializeNormalProperty(property,value))
);
}
Where CleverSwitch works similarly to JaredPar's TypeSwitch at the above link - and could be coded as follows:
static class CleverSwitch {
public class CaseInfo {
public Func<bool> Condition { get; set; }
public Action Action { get; set; }
}
public static void Do(object source, params CaseInfo[] cases) {
var type = source.GetType();
foreach (var entry in cases) {
if (entry.Condition()) {
entry.Action();
break;
}
}
}
public static CaseInfo IsType<T>(Type T2, Action action) {
return new CaseInfo() { Condition = () => T2 == typeof(T), Action = action };
}
public static CaseInfo If(Func<bool> condition, Action action) {
return new CaseInfo() { Condition = condition, Action = action };
}
public static CaseInfo Default(Action action) {
return new CaseInfo() { Condition = () => true, Action = action };
}
}
But on balance:
Any class to help you refactor it in the manner of the dictionary approach is going to have to be rather general in this case, and so essentially just be confusing and add performance overheads.
So whilst the above code should work, I really can't see how it improves on an if statement in any of the following areas:
time to write
readability or
maintainability
In fact, I'd argue that a simple if, return or if, else combination is more readable, maintainable and easier to write straight off... But that's just my two cents!

Cleaner way to do a null check in C#? [duplicate]

This question already has answers here:
C# elegant way to check if a property's property is null
(20 answers)
Closed 9 years ago.
Suppose, I have this interface,
interface IContact
{
IAddress address { get; set; }
}
interface IAddress
{
string city { get; set; }
}
class Person : IPerson
{
public IContact contact { get; set; }
}
class test
{
private test()
{
var person = new Person();
if (person.contact.address.city != null)
{
//this will never work if contact is itself null?
}
}
}
Person.Contact.Address.City != null (This works to check if City is null or not.)
However, this check fails if Address or Contact or Person itself is null.
Currently, one solution I could think of was this:
if (Person != null && Person.Contact!=null && Person.Contact.Address!= null && Person.Contact.Address.City != null)
{
// Do some stuff here..
}
Is there a cleaner way of doing this?
I really don't like the null check being done as (something == null). Instead, is there another nice way to do something like the something.IsNull() method?
In a generic way, you may use an expression tree and check with an extension method:
if (!person.IsNull(p => p.contact.address.city))
{
//Nothing is null
}
Full code:
public class IsNullVisitor : ExpressionVisitor
{
public bool IsNull { get; private set; }
public object CurrentObject { get; set; }
protected override Expression VisitMember(MemberExpression node)
{
base.VisitMember(node);
if (CheckNull())
{
return node;
}
var member = (PropertyInfo)node.Member;
CurrentObject = member.GetValue(CurrentObject,null);
CheckNull();
return node;
}
private bool CheckNull()
{
if (CurrentObject == null)
{
IsNull = true;
}
return IsNull;
}
}
public static class Helper
{
public static bool IsNull<T>(this T root,Expression<Func<T, object>> getter)
{
var visitor = new IsNullVisitor();
visitor.CurrentObject = root;
visitor.Visit(getter);
return visitor.IsNull;
}
}
class Program
{
static void Main(string[] args)
{
Person nullPerson = null;
var isNull_0 = nullPerson.IsNull(p => p.contact.address.city);
var isNull_1 = new Person().IsNull(p => p.contact.address.city);
var isNull_2 = new Person { contact = new Contact() }.IsNull(p => p.contact.address.city);
var isNull_3 = new Person { contact = new Contact { address = new Address() } }.IsNull(p => p.contact.address.city);
var notnull = new Person { contact = new Contact { address = new Address { city = "LONDON" } } }.IsNull(p => p.contact.address.city);
}
}
Your code may have bigger problems than needing to check for null references. As it stands, you are probably violating the Law of Demeter.
The Law of Demeter is one of those heuristics, like Don't Repeat Yourself, that helps you write easily maintainable code. It tells programmers not to access anything too far away from the immediate scope. For example, suppose I have this code:
public interface BusinessData {
public decimal Money { get; set; }
}
public class BusinessCalculator : ICalculator {
public BusinessData CalculateMoney() {
// snip
}
}
public BusinessController : IController {
public void DoAnAction() {
var businessDA = new BusinessCalculator().CalculateMoney();
Console.WriteLine(businessDA.Money * 100d);
}
}
The DoAnAction method violates the Law of Demeter. In one function, it accesses a BusinessCalcualtor, a BusinessData, and a decimal. This means that if any of the following changes are made, the line will have to be refactored:
The return type of BusinessCalculator.CalculateMoney() changes.
The type of BusinessData.Money changes
Considering the situation at had, these changes are rather likely to happen. If code like this is written throughout the codebase, making these changes could become very expensive. Besides that, it means that your BusinessController is coupled to both the BusinessCalculator and the BusinessData types.
One way to avoid this situation is rewritting the code like this:
public class BusinessCalculator : ICalculator {
private BusinessData CalculateMoney() {
// snip
}
public decimal CalculateCents() {
return CalculateMoney().Money * 100d;
}
}
public BusinessController : IController {
public void DoAnAction() {
Console.WriteLine(new BusinessCalculator().CalculateCents());
}
}
Now, if you make either of the above changes, you only have to refactor one more piece of code, the BusinessCalculator.CalculateCents() method. You've also eliminated BusinessController's dependency on BusinessData.
Your code suffers from a similar issue:
interface IContact
{
IAddress address { get; set; }
}
interface IAddress
{
string city { get; set; }
}
class Person : IPerson
{
public IContact contact { get; set; }
}
class Test {
public void Main() {
var contact = new Person().contact;
var address = contact.address;
var city = address.city;
Console.WriteLine(city);
}
}
If any of the following changes are made, you will need to refactor the main method I wrote or the null check you wrote:
The type of IPerson.contact changes
The type of IContact.address changes
The type of IAddress.city changes
I think you should consider a deeper refactoring of your code than simply rewriting a null check.
That said, I think that there are times where following the Law of Demeter is inappropriate. (It is, after all, a heuristic, not a hard-and-fast rule, even though it's called a "law.")
In particular, I think that if:
You have some classes that represent records stored in the persistence layer of your program, AND
You are extremely confident that you will not need to refactor those classes in the future,
ignoring the Law of Demeter is acceptable when dealing specifically with those classes. This is because they represent the data your application works with, so reaching from one data object into another is a way of exploring the information in your program. In my example above, the coupling caused by violating the Law of Demeter was much more severe: I was reaching all the way from a controller near the top of my stack through a business logic calculator in the middle of the stack into a data class likely in the persistence layer.
I bring this potential exception to the Law of Demeter up because with names like Person, Contact, and Address, your classes look like they might be data-layer POCOs. If that's the case, and you are extremely confident that you will never need to refactor them in the future, you might be able to get away with ignoring the Law of Demeter in your specific situation.
in your case you could create a property for person
public bool HasCity
{
get
{
return (this.Contact!=null && this.Contact.Address!= null && this.Contact.Address.City != null);
}
}
but you still have to check if person is null
if (person != null && person.HasCity)
{
}
to your other question, for strings you can also check if null or empty this way:
string s = string.Empty;
if (!string.IsNullOrEmpty(s))
{
// string is not null and not empty
}
if (!string.IsNullOrWhiteSpace(s))
{
// string is not null, not empty and not contains only white spaces
}
A totally different option (which I think is underused) is the null object pattern. It's hard to tell whether it makes sense in your particular situation, but it might be worth a try. In short, you will have a NullContact implementation, a NullAddress implementation and so on that you use instead of null. That way, you can get rid of most of the null checks, of course at the expense at some thought you have to put into the design of these implementations.
As Adam pointed out in his comment, this allows you to write
if (person.Contact.Address.City is NullCity)
in cases where it is really necessary. Of course, this only makes sense if city really is a non-trivial object...
Alternatively, the null object can be implemented as a singleton (e.g., look here for some practical instructions concerning the usage of the null object pattern and here for instructions concerning singletons in C#) which allows you to use classical comparison.
if (person.Contact.Address.City == NullCity.Instance)
Personally, I prefer this approach because I think it is easier to read for people not familiar with the pattern.
Update 28/04/2014: Null propagation is planned for C# vNext
There are bigger problems than propagating null checks. Aim for readable code that can be understood by another developer, and although it's wordy - your example is fine.
If it is a check that is done frequently, consider encapsulating it inside the Person class as a property or method call.
That said, gratuitous Func and generics!
I would never do this, but here is another alternative:
class NullHelper
{
public static bool ChainNotNull<TFirst, TSecond, TThird, TFourth>(TFirst item1, Func<TFirst, TSecond> getItem2, Func<TSecond, TThird> getItem3, Func<TThird, TFourth> getItem4)
{
if (item1 == null)
return false;
var item2 = getItem2(item1);
if (item2 == null)
return false;
var item3 = getItem3(item2);
if (item3 == null)
return false;
var item4 = getItem4(item3);
if (item4 == null)
return false;
return true;
}
}
Called:
static void Main(string[] args)
{
Person person = new Person { Address = new Address { PostCode = new Postcode { Value = "" } } };
if (NullHelper.ChainNotNull(person, p => p.Address, a => a.PostCode, p => p.Value))
{
Console.WriteLine("Not null");
}
else
{
Console.WriteLine("null");
}
Console.ReadLine();
}
The second question,
I really don't like the null check being done as (something == null). Instead, is there another nice way to do something like the something.IsNull() method?
could be solved using an extension method:
public static class Extensions
{
public static bool IsNull<T>(this T source) where T : class
{
return source == null;
}
}
If for some reason you don't mind going with one of the more 'over the top' solutions, you might want to check out the solution described in my blog post. It uses the expression tree to find out whether the value is null before evaluating the expression. But to keep performance acceptable, it creates and caches IL code.
The solution allows you do write this:
string city = person.NullSafeGet(n => n.Contact.Address.City);
You can write:
public static class Extensions
{
public static bool IsNull(this object obj)
{
return obj == null;
}
}
and then:
string s = null;
if(s.IsNull())
{
}
Sometimes this makes sense. But personally I would avoid such things... because this is is not clear why you can call a method of the object that is actually null.
Do it in a separate method like:
private test()
{
var person = new Person();
if (!IsNull(person))
{
// Proceed
........
Where your IsNull method is
public bool IsNull(Person person)
{
if(Person != null &&
Person.Contact != null &&
Person.Contact.Address != null &&
Person.Contact.Address.City != null)
return false;
return true;
}
Do you need C#, or do you only want .NET? If you can mix another .NET language, have a look at Oxygene. It's an amazing, very modern OO language that targets .NET (and also Java and Cocoa as well. Yep. All natively, it really is quite an amazing toolchain.)
Oxygene has a colon operator which does exactly what you ask. To quote from their miscellaneous language features page:
The Colon (":") Operator
In Oxygene, like in many of the languages it
was influenced by, the "." operator is used to call members on a class
or object, such as
var x := y.SomeProperty;
This "dereferences" the object contained in
"y", calls (in this case) the property getter and returns its value.
If "y" happens to be unassigned (i.e. "nil"), an exception is thrown.
The ":" operator works in much the same way, but instead of throwing
an exception on an unassigned object, the result will simply be nil.
For developers coming from Objective-C, this will be familiar, as that
is how Objective-C method calls using the [] syntax work, too.
... (snip)
Where ":" really shines is when accessing properties in a chain, where
any element might be nil. For example, the following code:
var y := MyForm:OkButton:Caption:Length;
will run without error, and
return nil if any of the objects in the chain are nil — the form, the
button or its caption.
try
{
// do some stuff here
}
catch (NullReferenceException e)
{
}
Don't actually do this. Do the null checks, and figure out what formatting you can best live with.
I have an extension that could be useful for this; ValueOrDefault(). It accepts a lambda statement and evaluates it, returning either the evaluated value or a default value if any expected exceptions (NRE or IOE) are thrown.
/// <summary>
/// Provides a null-safe member accessor that will return either the result of the lambda or the specified default value.
/// </summary>
/// <typeparam name="TIn">The type of the in.</typeparam>
/// <typeparam name="TOut">The type of the out.</typeparam>
/// <param name="input">The input.</param>
/// <param name="projection">A lambda specifying the value to produce.</param>
/// <param name="defaultValue">The default value to use if the projection or any parent is null.</param>
/// <returns>the result of the lambda, or the specified default value if any reference in the lambda is null.</returns>
public static TOut ValueOrDefault<TIn, TOut>(this TIn input, Func<TIn, TOut> projection, TOut defaultValue)
{
try
{
var result = projection(input);
if (result == null) result = defaultValue;
return result;
}
catch (NullReferenceException) //most reference types throw this on a null instance
{
return defaultValue;
}
catch (InvalidOperationException) //Nullable<T> throws this when accessing Value
{
return defaultValue;
}
}
/// <summary>
/// Provides a null-safe member accessor that will return either the result of the lambda or the default value for the type.
/// </summary>
/// <typeparam name="TIn">The type of the in.</typeparam>
/// <typeparam name="TOut">The type of the out.</typeparam>
/// <param name="input">The input.</param>
/// <param name="projection">A lambda specifying the value to produce.</param>
/// <returns>the result of the lambda, or default(TOut) if any reference in the lambda is null.</returns>
public static TOut ValueOrDefault<TIn, TOut>(this TIn input, Func<TIn, TOut> projection)
{
return input.ValueOrDefault(projection, default(TOut));
}
The overload not taking a specific default value will return null for any reference type. This should work in your scenario:
class test
{
private test()
{
var person = new Person();
if (person.ValueOrDefault(p=>p.contact.address.city) != null)
{
//the above will return null without exception if any member in the chain is null
}
}
}
Such a reference chain may occurre for example if you use an ORM tool, and want to keep your classes as pure as possible. In this scenario I think it cannot be avoided nicely.
I have the following extension method "family", which checks if the object on which it's called is null, and if not, returns one of it's requested properties, or executes some methods with it. This works of course only for reference types, that's why I have the corresponding generic constraint.
public static TRet NullOr<T, TRet>(this T obj, Func<T, TRet> getter) where T : class
{
return obj != null ? getter(obj) : default(TRet);
}
public static void NullOrDo<T>(this T obj, Action<T> action) where T : class
{
if (obj != null)
action(obj);
}
These methods add almost no overhead compared to the manual solution (no reflection, no expression trees), and you can achieve a nicer syntax with them (IMO).
var city = person.NullOr(e => e.Contact).NullOr(e => e.Address).NullOr(e => e.City);
if (city != null)
// do something...
Or with methods:
person.NullOrDo(p => p.GoToWork());
However, one could definetely argue about the length of code didn't change too much.
In my opinion, the equality operator is not a safer and better way for reference equality.
It's always better to use ReferenceEquals(obj, null). This will always work. On the other hand, the equality operator (==) could be overloaded and might be checking if the values are equal instead of the references, so I will say ReferenceEquals() is a safer and better way.
class MyClass {
static void Main() {
object o = null;
object p = null;
object q = new Object();
Console.WriteLine(Object.ReferenceEquals(o, p));
p = q;
Console.WriteLine(Object.ReferenceEquals(p, q));
Console.WriteLine(Object.ReferenceEquals(o, p));
}
}
Reference: MSDN article Object.ReferenceEquals Method.
But also here are my thoughts for null values
Generally, returning null values is the best idea if anyone is trying to indicate that there is no data.
If the object is not null, but empty, it implies that data has been returned, whereas returning null clearly indicates that nothing has been returned.
Also IMO, if you will return null, it will result in a null exception if you attempt to access members in the object, which can be useful for highlighting buggy code.
In C#, there are two different kinds of equality:
reference equality and
value equality.
When a type is immutable, overloading operator == to compare value equality instead of reference equality can be useful.
Overriding operator == in non-immutable types is not recommended.
Refer to the MSDN article Guidelines for Overloading Equals() and Operator == (C# Programming Guide) for more details.
As much as I love C#, this is one thing that's kind of likable about C++ when working directly with object instances; some declarations simply cannot be null, so there's no need to check for null.
The best way you can get a slice of this pie in C# (which might be a bit too much redesigning on your part - in which case, take your pick of the other answers) is with struct's. While you could find yourself in a situation where a struct has uninstantiated "default" values (ie, 0, 0.0, null string) there's never a need to check "if (myStruct == null)".
I wouldn't switch over to them without understanding their use, of course. They tend to be used for value types, and not really for large blocks of data - anytime you assign a struct from one variable to another, you tend to be actually copying the data across, essentially creating a copy of each of the original's values (you can avoid this with the ref keyword - again, read up on it rather than just using it). Still, it may fit for things like StreetAddress - I certainly wouldn't lazily use it on anything I didn't want to null-check.
Depending on what the purpose of using the "city" variable is, a cleaner way could be to separate the null checks into different classes. That way you also wouldn't be violating the Law of Demeter. So instead of:
if (person != null && person.contact != null && person.contact.address != null && person.contact.address.city != null)
{
// do some stuff here..
}
You'd have:
class test
{
private test()
{
var person = new Person();
if (person != null)
{
person.doSomething();
}
}
}
...
/* Person class */
doSomething()
{
if (contact != null)
{
contact.doSomething();
}
}
...
/* Contact class */
doSomething()
{
if (address != null)
{
address.doSomething();
}
}
...
/* Address class */
doSomething()
{
if (city != null)
{
// do something with city
}
}
Again, it depends on the purpose of the program.
In what circumstances can those things be null? If nulls would indicate a bug in the code then you could use code contracts. They will pick it up if you get nulls during testing, then will go away in the production version. Something like this:
using System.Diagnostics.Contracts;
[ContractClass(typeof(IContactContract))]
interface IContact
{
IAddress address { get; set; }
}
[ContractClassFor(typeof(IContact))]
internal abstract class IContactContract: IContact
{
IAddress address
{
get
{
Contract.Ensures(Contract.Result<IAddress>() != null);
return default(IAddress); // dummy return
}
}
}
[ContractClass(typeof(IAddressContract))]
interface IAddress
{
string city { get; set; }
}
[ContractClassFor(typeof(IAddress))]
internal abstract class IAddressContract: IAddress
{
string city
{
get
{
Contract.Ensures(Contract.Result<string>() != null);
return default(string); // dummy return
}
}
}
class Person
{
[ContractInvariantMethod]
protected void ObjectInvariant()
{
Contract.Invariant(contact != null);
}
public IContact contact { get; set; }
}
class test
{
private test()
{
var person = new Person();
Contract.Assert(person != null);
if (person.contact.address.city != null)
{
// If you get here, person cannot be null, person.contact cannot be null
// person.contact.address cannot be null and person.contact.address.city cannot be null.
}
}
}
Of course, if the possible nulls are coming from somewhere else then you'll need to have already conditioned the data. And if any of the nulls are valid then you shouldn't make non-null a part of the contract, you need to test for them and handle them appropriately.
One way to remove null checks in methods is to encapsulate their functionality elsewhere. One way to do this is through getters and setters. For instance, instead of doing this:
class Person : IPerson
{
public IContact contact { get; set; }
}
Do this:
class Person : IPerson
{
public IContact contact
{
get
{
// This initializes the property if it is null.
// That way, anytime you access the property "contact" in your code,
// it will check to see if it is null and initialize if needed.
if(_contact == null)
{
_contact = new Contact();
}
return _contact;
}
set
{
_contact = value;
}
}
private IContact _contact;
}
Then, whenever you call "person.contact", the code in the "get" method will run, thus initializing the value if it is null.
You could apply this exact same methodology to all of the properties that could be null across all of your types. The benefits to this approach are that it 1) prevents you from having to do null checks in-line and it 2) makes your code more readable and less prone to copy-paste errors.
It should be noted, however, that if you find yourself in a situation where you need to perform some action if one of the properties is null (i.e. does a Person with a null Contact actually mean something in your domain?), then this approach will be a hindrance rather than a help. However, if the properties in question should never be null, then this approach will give you a very clean way of representing that fact.
--jtlovetteiii
You could use reflection, to avoid forcing implementation of interfaces and extra code in every class. Simply a Helper class with static method(s). This might not be the most efficient way, be gentle with me, I'm a virgin (read, noob)..
public class Helper
{
public static bool IsNull(object o, params string[] prop)
{
if (o == null)
return true;
var v = o;
foreach (string s in prop)
{
PropertyInfo pi = v.GetType().GetProperty(s); //Set flags if not only public props
v = (pi != null)? pi.GetValue(v, null) : null;
if (v == null)
return true;
}
return false;
}
}
//In use
isNull = Helper.IsNull(p, "ContactPerson", "TheCity");
Offcourse if you have a typo in the propnames, the result will be wrong (most likely)..

How to check if an object is not of a particular type?

I want to check if an object is not of a particular type. I know how to check if something is of a particular type:
if (t is TypeA)
{
...
}
but
if (t isnt TypeA)
{
...
}
doesn't work.
UPDATE 2020-10-30:
Times are changing. Starting from C# 9.0 you can use more natural way of checking it:
if(t is not TypeA) { ... }
ORIGINAL ANSWER:
C# is not quite natural language ;)
Use this one
if(!(t is TypeA))
{
...
}
if you want not only check, you can use as operator.
var a = t as TypeA;
if(a!= null)
//use a..
In this way, if you want use a type after check, you avoid double casting..
Extensions methods to the rescue!!
public static class ObjectExtensions
{
public static bool Isnt(this object source, Type targetType)
{
return source.GetType() != targetType;
}
}
Usage
if (t.Isnt(typeof(TypeA)))
{
...
}
If you are doing a TypeA x = (TypeA)t; inside the if block then a better way is
TypeA x = t as TypeA
if(x != null)
{
...
}
This causes only one time type checking rather than twice.
Short answer: you may want to use:
if (t.GetType()==typeof(TypeA))
{
...
}
if (t.GetType()!=typeof(TypeA))
{
...
}
Long answer:
So. Be aware that you're asking if it's a particular type. is doesn't tell you if it's a particular type - it tells you if it's a particular type or any descendant of that type.
So if you have two classes, Animal, and Cat : Animal, and felix is a cat, then
if (felix is Animal)
{
//returns true
}
if (felix.GetType() == typeof(Animal))
{
//does not
}
If it's not important to you, inherited classes are okay, then don't worry about it, use !(felix is Animal) as others mentioned, and you're fine! (You're probably fine.)
But if you need to be sure felix is specifically an Animal, then you need to ask if t.GetType()==typeof(TypeA).
I usually stick the null and type checking all in one line:
if (t == null || !(t is TypeA)) {
...
}
If TypeA is a struct, you'll need to handle it slightly differently again:
if (t == null || t.GetType() != typeof(TypeA)) {
...
}
Check below example for getType():
object obj = new object();
obj.GetType();
string s;
s.GetType();
List<string> StringList = new List<string>();
StringList.GetType();

Categories

Resources