0

In JavaScript we can OR two objects/properties like this:-

var myURL = window.URL || window.webkitURL;

also

function doKey(e) {
    var evt = e || window.event; // compliant with ie6      
    //Code
}

Can we OR C# class objects?

SpecificClass1 sc1 = new SpecificClass1();
SpecificClass2 sc2 = new SpecificClass2();
sc2 = null;

var temp = sc1 || sc2;

Edit:-

Not working for me using ??

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MultipleCast
{
    class Program
    {

        public class BaseClass { 
        }
        public class SpecificClass1 : BaseClass
        {
            public string SpecificMethod1()
            {
                return "SpecificMethod1";
            }

        }
        public class SpecificClass2 : BaseClass
        {
            public string SpecificMethod2()
            {
                return "SpecificMethod2";
            }
        }
        public class SpecificClass3 : BaseClass
        {
            public string SpecificMethod3()
            {
                return "SpecificMethod3";
            }
        }

        static void Main(string[] args)
        {

            SpecificClass1 sc1 = new SpecificClass1();
            SpecificClass2 sc2 = new SpecificClass2();
            sc1 = null;

            var temp = sc1 ?? sc2;

            Console.WriteLine(temp);

            Console.Read();

        }
    }
}

Error:-

Operator '??' cannot be applied to operands of type 'MultipleCast.Program.SpecificClass1' and 'MultipleCast.Program.SpecificClass2'

Aditya Singh
  • 9,512
  • 5
  • 32
  • 55
  • The compiler can't infer which type you want the `temp` variable to be (since `sc1` and `sc2` is of different types). You have to declare your `temp` variable as `BaseClass temp = sc1 ?? sc2;`. – khellang Feb 24 '14 at 13:33

2 Answers2

6

The null-coalescing operator (??) will do this for you:

var temp = sc1 ?? sc2;

If sc1 is null, temp will be sc2.

khellang
  • 17,550
  • 6
  • 64
  • 84
-2

If you overwrite the operator for that class, then you can do it and it will do exactly what you want.

s. here how to do it: http://msdn.microsoft.com/en-us/library/aa288467(v=vs.71).aspx

  • ah, ok, it's different types... then my suggestion would only work if they had one common base class and the operator overload was there... if this is only about simple NULL checks then the question is not formed well... – mindfxxxedCoder Feb 24 '14 at 14:13
  • How is the question not formed well? He's asking if there's anything in C# that behaves (more or less) the same as JavaScript's OR operator, not about simple `null` checks or if it's possible to implement something to get it to work. – khellang Feb 24 '14 at 22:13
  • Then I do not get the part about the ?? and what this has to do with a boolean OR operator? Quote: The ?? operator is called the null-coalescing operator. It returns the left-hand operand if the operand is not null; otherwise it returns the right hand operand. This is NOT a boolean OR. So the answer is either wrong or the question is not good. And I'd still like to know what the problem is wth overriding the operator manually. – mindfxxxedCoder Feb 25 '14 at 08:13
  • You obviously don't know JavaScript. The null-coalescing operator in C# works almost exectly like the logical OR operator in JavaScript. See this answer: http://stackoverflow.com/questions/476436/is-there-a-null-coalescing-operator-in-javascript/476445#476445. The problem with overriding the operator is that you have to do it for all types where you want to use the functionality. It's not something the language offers. – khellang Feb 25 '14 at 09:20
  • I really know nearly nothing about javascript, but I still see a lot of difference between C# ?? and JavaScript OR when I look at it... of course you have to implement overrided operators for every type that you want to use it for, but then at least it does what it is supposed to do. Nearly all of the "var whatIWant" examples that are listed in the link you just provided cannot be replaced with ??, besides the 1st one, so call me stubborn, but I still think my idea is not so bad that it should be voted down and I still think that your answer is still not completely correct. but lets leave it – mindfxxxedCoder Feb 25 '14 at 09:35