C# is an Object-oriented programming language. C# comes with simplicity, expressiveness and great performance to meet the programmer productivity.
Introduction
So far we have seen
Conversions, Value types and Reference types in C#. Now let's look into Numeric types in this chapter.
Objective
The main objective of this article is to learn about Null literal, Numeric types and Numeric literals in
C# programming.
Null Literal
Null literal is null type. It means, it indicates that the reference points to no object. Let's have an example code as below,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Man
{
class Program
{
public class pen
{
public int ink, cap;
}
static void Main()
{
pen p = null;
Console.WriteLine(p == null); // This will print 'True'
}
}
}In the above code we have a class
pen with two variables
ink and
cap.
Now run this simple code and see the output,
If you replace the Console.WriteLine as below,
Console.WriteLine(p != null); // This will print 'False'
This will prints
False in the output as,
And, If you add the below line to the above code, it will generate a runtime error.
Console.WriteLine(p.ink); // Below line will generate NullReference Exception
The
error generates like below in the runtime,
NullReferenceException is thrown in the above case.
Numeric Types
C# has some predefined numeric types which uses System namespace. They are categorized as follows,
Signed Integrals
| Numeric Type | Size | Range | .NET Framework Type |
| sbyte | 8 bits | -27 to 27-1 | System.SByte |
| short | 16 bits | -215 to 215-1 | System.Int16 |
| int | 32 bits | -231 to 231-1 | System.Int32 |
| long | 64 bits | -263 to 263-1 | System.Int64 |
Observe the above table in which the
Signed Integers allows the negative values as well as positive values.
Unsigned Integrals
| Numeric Type | Size | Range | .NET Framework Type |
| byte | 8 bits | 0 to 28-1 | System.Byte |
| ushort | 16 bits | 0 to 216-1 | System.UInt16 |
| uint | 32 bits | 0 to 232-1 | System.UInt32 |
| ulong | 64 bits | 0 to 264-1 | System.UInt64 |
Unsigned Integers doesn't allow negative values but they allows great positive ones as mentioned in the above table.
Real
| Numeric Type | Size | Range | .NET Framework Type |
| float | 32 bits | ±(~10-45 to 1038) | System.Single |
| double | 64 bits | ±(~10-324 to 10308) | System.Double |
| decimal | 128 bits | ±(~1031 to 1028) | System.Decimal |
Among all the Integral types above, int and long are first-class keywords which are favored by C# as well as runtime. Other types are mostly used for interoperability.
And in the Real types, float and double are called as floating-point types and mostly used for scientific calculations.
Other Built-In Types
There are some other predefined C# Types.
| C# Type | Description | .NET Framework Type |
| bool | Used for variable declaration to store Boolean values, true and false | System.Boolean |
| char | Used to represent a Unicode character. Value is 16-bit numeric (ordinal) value. | System.Char |
| string | Represents a series of Unicode Characters (might be zero). | System.String |
| object | All types such as user-defined, predefined, reference types and value types inherit directly or indirectly from Object. | System.Object |
Numeric Literals
There are two types of numeric literals, integral literals and real literals.
Integral Literals:
These literals use decimal or hexadecimal notation. Hexadecimal is denoted by the prefix '0x'.
Ex:
int a = 45; //Decimal value
long b = 0x6B; //Hexadecimal value
Real Literals:
These literals use decimal or exponential notation and sometimes both.
Ex:
double c = 4.25 //Decimal value
double thousand = 1E03 //Exponential value equals to 1000
Conclusion
In this article, we have seen what is
null literal with example and
numeric types in C#. Hope you understand.
Thanks for reading.
Regards,
Krishna.