Null literal, Numeric types and Numeric literals in C#

Goud.Kv
Posted by in C# category on for Beginner level | Points: 250 | Views : 5144 red flag

C# is an Object-oriented programming language. C# comes with simplicity, expressiveness and great performance to meet the programmer productivity.
Recommendation
Read Conversions, CTS, Value and Reference types in C# before this article.

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 TypeSizeRange.NET Framework Type
sbyte8 bits-2to 27-1System.SByte
short16 bits-215 to 215-1System.Int16
int32 bits-231 to 231-1System.Int32
long64 bits-263 to 263-1System.Int64
Observe the above table in which the Signed Integers allows the negative values as well as positive values.

Unsigned Integrals
Numeric TypeSizeRange.NET Framework Type
byte8 bits0 to 28-1System.Byte
ushort16 bits0 to 216-1System.UInt16
uint32 bits0 to 232-1System.UInt32
ulong64 bits0 to 264-1System.UInt64
Unsigned Integers doesn't allow negative values but they allows great positive ones as mentioned in the above table.

Real
Numeric TypeSizeRange.NET Framework Type
float32 bits±(~10-45 to 1038)System.Single
double64 bits±(~10-324 to 10308)System.Double
decimal128 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# TypeDescription.NET Framework Type
boolUsed for variable declaration to store Boolean values, true and falseSystem.Boolean
charUsed to represent a Unicode character. Value is 16-bit numeric (ordinal) value.System.Char
stringRepresents a series of Unicode Characters (might be zero).System.String
objectAll 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.

Page copy protected against web site content infringement by Copyscape

About the Author

Goud.Kv
Full Name: Krishna Vamshi Goud
Member Level: Gold
Member Status: Member,MVP
Member Since: 2/12/2014 2:34:09 AM
Country: India
Thanks & Regards, Krishna


Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)