This video looks at some of the new C# 7 enhancements with constants for binary values and formatting.
[Narrator] There are two improvements that have been made to constants in C Sharp 7. The improvements are the ability to create binary constants, and also to format numeric constants. So I'm going to open up the FeatureValueConverter, and this is under OldCars, Converters, FeatureValueConverter. Let's scroll down a little bit so we can see the convert method starting on line 10. We can see a bitwise operation being used on the integer value passed in. In the value converter, we are building a string based on the bitwise comparison with the value being passed in and a constant in the FeatureConstants class.
We know that under the covers, this code is doing comparison to see if some of the integers' bits in particular positions are set in the same places as the bits in the constant. Let's take a look at those values, and see what is store there. So I'm going to look at Constants, and FeatureConstants. Here are the definitions for constants used in the bitwise operation: zero, one, two, four, eight, 16, 32, 64. The numeric sequence is obvious, but it does mask what the bitwise operation is doing under the hood.
This is why many people prefer using explicit binary values. Let's convert to binary values instead. So here we have zero. I can switch that to zero b, and I'm going to put after that eight zeroes. Zero b is the prefix for a binary constant. We started with an integer zero, and that being equivalent to all zeroes on a binary level is obvious. Let's go ahead and replace the rest of the values.
I'm going to copy this here. And push this in right here, and this is actually one here, for replacing the one. Let's space it out so it's obvious what's happening. And we'll do the same with the two, and it's actually the second position that's changed, and space that back out. And here we're just counting out in binary and putting in the proper values for each of these, and as you can see as I go down, we're lining up where the ones appear in the underlying binary expression.
Now this is making the bitwise operation a little more clear on exactly what it's comparing here. And here we'll fix the last one up. And that's actually binary 64. And here now we can see exactly what's going on underneath the hood. A bitwise check to see if a value contains the values for radio and automatic transmission will be a number where the two right-most digits are one.
So for example, if I had a binary representation, and let's just take this a put the number here, of this. That would indicate that it has a radio, and automatic. And let me delete that. One thing you may have noticed is that while I was coding out the zeroes, to put one in the right spot it was difficult due to the amount of zeroes involved. We can now format to make this easier. We can place an underscore anywhere in a numeric constant to make it easier to read.
Our binary numbers are eight digits long. Let's make a separator between the first four bits and the second four. So I'll continue down just like that. And now it's a little easier to read these numbers than it had been. With the underscore in place, it is easier to mentally process the position of where the ones are in the string of zeroes. We can do similar formatting with any numeric constant. Let's add a constant for the US population, which at the time of doing this class, is around 323.95 million.
So let's add that constant in, and what we might be able to do with it now, so we'll go Public, and Constant, and that will also be an int, and it will be the US population. And we said that US population was 323 million, 950 thousand, just like that. And that's relatively easy to read. The underscore can be used as needed to enhance the readability of your values. The underscore can be used in numeric constants that contain decimal values, like doubles or floats.
It can be placed before or after a decimal place. It could be used to delineate the decimal places in a long constant for Pi. The changes to C Sharp to numeric constants enhance the maintainability of your applications by adding binary constants, as well as making long numeric values easier to read.
- Working with tuples
- Using C# throw expressions
- Inspecting UI structures with Xamarin Inspector
- Identifying memory leaks in iOS and Android
- Recording and playing back tests with Test Record
- Using embedded assemblies in Android