473,387 Members | 1,611 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,387 developers and data experts.

?? and ?: -- Somewhat lesser known operators

Curtis Rutland
3,256 Expert 2GB
I've known about these beauties for quite some time, but never thought of sharing them until now. They're a bit obscure, and I want to help remedy that.
================================================== =====

?? is a C# binary operator used to provide a default value should the left operand be null. For example:

Expand|Select|Wrap|Line Numbers
  1. X = A ?? B;
equates to:
Expand|Select|Wrap|Line Numbers
  1. if(A != null)
  2.   X = A;
  3. else
  4.   X = B;
Relatively simple, but a great time saver, and sometimes quite useful. Another example:
Expand|Select|Wrap|Line Numbers
  1. string result = ( TextBox1 ?? new TextBox() ).Text;
In this example, if TextBox1 is not null, result will contain TextBox1.Text. Otherwise, it will contain a blank string, since that is what new TextBoxes are initialized to.
================================================== =====

?: is a C# (and many other languages) ternary operator used to return a value based on the evaluation of a boolean expression. This one is far more commonly known than the ?? operator, because it has its roots in C, but I'm still surprised at how many people don't know how to use it. Here's an example of how it works:

Expand|Select|Wrap|Line Numbers
  1. X = A ? B : C;
This is equivalent to:
Expand|Select|Wrap|Line Numbers
  1. if(A)
  2.   X = B;
  3. else
  4.   X = C;
A more practical example:
Expand|Select|Wrap|Line Numbers
  1. string substr = fullstr.Length > 10 ? fullstr.Substring(0, 10) : fullstr;
This code sets substr's value to the first 10 characters of fullstr, unless fullstr is shorter than 10 characters, in which case, substr is assigned the value of fullstr.
================================================== =====

Hope you found this enlightening!
May 19 '10 #1
4 3330
ThatThatGuy
449 Expert 256MB
Cool, i didn't knew about the first one ??
May 20 '10 #2
Plater
7,872 Expert 4TB
Yeah, the first one is new on me, and very useful.

Note on the 2nd one:
X = A ? B : C;
A can be expanded to compound boolean statements but you need to wrap the whole package in ( )
X= (Aa && Ab)? B : C;
May 20 '10 #3
Curtis Rutland
3,256 Expert 2GB
I knew the inline-if statement was pretty well known, if less used, because it's been around since C, if not before.

But the null coalescing operator is practically unknown, and it's great.
May 20 '10 #4
Bassem
344 100+
Nice topic. ?? is new to me. Good to add that
The ?? operator defines the default value to be returned when a nullable type is assigned to a non-nullable type. If you try to assign a nullable value type to a non-nullable value type without using the ?? operator, you will generate a compile-time error.
From the link that insertAlias provided.
Aug 2 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: N4M | last post by:
Dear, I have problems with overloaded operators ++() and --(). MSVC++ 6.0 compiler gives errors, one is shown as below: " c:\data\c++\mygraphs\graph.h(182) : error C2555: 'CGraphNodeIter::++' :...
5
by: Lionel B | last post by:
Greetings, I am trying to implement "element-wise" arithmetic operators for a class along the following lines (this is a simplified example): // ----- BEGIN CODE ----- struct X { int a,b;
1
by: DiskMan | last post by:
System: Redhat 7.2 Kernel-2.6.11.8 GCC-3.4.3 CCC-6.5.9 Binutils-2.15 Make-3.80 GTK/GLIB-2.6.7 For some reason my Linux box is suddenly having issues trying to read ;
11
by: Micha | last post by:
Hello there, I think I've run into some classic c++ pitfall and maybe some of you guys can help me out. For my project I will need to use matrices and vectors and so I decided to implement them...
6
by: jas_lx | last post by:
The basic understanding of what bitwise operators (& ^ | >> << ) comes fairly simple, as long as one has a fundamental understanding of bits, bytes and binary. Having done some Win32...
7
by: anjogasa | last post by:
In the midst of reading "The C++ Programming Language", the latest edition by Stroustrup, and I find myself butting my head against a few paragraphs. I have searched the errata on the website, and...
10
by: Jonathan | last post by:
Hi all, I'm having an argument with a co-worker about the difference between the & and && operators when applied to boolean operands in C#. His point of view is that the expression (false &...
9
by: John | last post by:
I'm sorry if this is sounding like somewhat of a noob question. I'm loading in a large binary array of 8x8 double precision floating point matrices, right now this is defined something like ...
7
by: AlanJSmith | last post by:
I am converting some C++ projects from 6 to vs2005 in one of the headers i have the code that follows this passage in which I get an error saying int default cant be assumed so i added...
10
by: ankitks | last post by:
Hello programmers, I am trying to write a class which has a two operators # and <=, so that I can do something like this. ... Array mArray; ..... #4 mArray <= 5; //is this possible to have...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.