InterviewSolution
| 1. |
Why Nullables are used in C#? |
|
Answer» Nullable are special data types that can be assigned normal data VALUES and null values as well. For example: Nullable<Int32> can be assigned an integer value and it can also be assigned null. The syntax for a nullable declaration in C# is given as follows: DataType? NameOfVariable = DataValue;In the above syntax, DataType is the data type such as int,char, float ETC. followed by ?. NameOfVariable is the variable name and the DataValue is the value assigned to the variable which can be null or any value of the specified data type. A program that demonstrates Nullables in C# is given as follows: using SYSTEM; namespace DEMO { class Example { static void Main(string[] args) { int? NUM1 = 4; int? num2 = null; Console.WriteLine("Value of num1 is: {0}", num1); Console.WriteLine("Value of num2 is: {0}", num2); } } }The output of the above program is as follows: Value of num1 is: 4 Value of num2 is: |
|