Csharp/C Sharp/Data Types/Nulllable
Содержание
Declare a nullable type by adding the ? type modifier in a value type declaration.
<source lang="csharp"> using System; public class MainClass {
double? variable1 = null; static void Main() { int? variablea = null; Console.WriteLine(variablea.HasValue); // false int variableb = variablea ?? 5; Console.WriteLine(variableb); // 5 }
}
</source>
Nullable bool Types
<source lang="csharp"> using System; using System.Collections.Generic; using System.Text; class DatabaseReader {
// Nullable data field. public int? numbericValue; public bool? boolValue = true; // Note the nullable return type. public int? GetIntFromDatabase() { return numbericValue; } // Note the nullable return type. public bool? GetBoolFromDatabase() { return boolValue; }
} class Program {
static void Main(string[] args) { DatabaseReader dr = new DatabaseReader(); int? i = dr.GetIntFromDatabase(); if (i.HasValue) Console.WriteLine("Value of "i" is: {0}", i); else Console.WriteLine("Value of "i" is undefined."); // Get bool from "database". bool? b = dr.GetBoolFromDatabase(); if (b != null) Console.WriteLine("Value of "b" is: {0}", b); else Console.WriteLine("Value of "b" is undefined."); // If the value from GetIntFromDatabase() is null, // assign local variable to 100. int? myData = dr.GetIntFromDatabase() ?? 100; Console.WriteLine("Value of myData: {0}", myData); }
}
</source>
Nullable int Types
<source lang="csharp"> using System; using System.Collections.Generic; using System.Text; class DatabaseReader {
// Nullable data field. public int? numbericValue; public bool? boolValue = true; // Note the nullable return type. public int? GetIntFromDatabase() { return numbericValue; } // Note the nullable return type. public bool? GetBoolFromDatabase() { return boolValue; }
} class Program {
static void Main(string[] args) { DatabaseReader dr = new DatabaseReader(); int? i = dr.GetIntFromDatabase(); if (i.HasValue) Console.WriteLine("Value of "i" is: {0}", i); else Console.WriteLine("Value of "i" is undefined."); // Get bool from "database". bool? b = dr.GetBoolFromDatabase(); if (b != null) Console.WriteLine("Value of "b" is: {0}", b); else Console.WriteLine("Value of "b" is undefined."); // If the value from GetIntFromDatabase() is null, // assign local variable to 100. int? myData = dr.GetIntFromDatabase() ?? 100; Console.WriteLine("Value of myData: {0}", myData); }
}
</source>
Nullable variable
<source lang="csharp">
using System; using System.Collections.Generic; using System.Text; public class Vector {
public double? R = null; public double? Theta = null; public double? ThetaRadians { get { return (Theta * Math.PI / 180.0); } } public Vector(double? r, double? theta) { if (r < 0) { r = -r; theta += 180; } theta = theta % 360; R = r; Theta = theta; } public static Vector operator +(Vector op1, Vector op2) { try { double newX = op1.R.Value * Math.Sin(op1.ThetaRadians.Value) + op2.R.Value * Math.Sin(op2.ThetaRadians.Value); double newY = op1.R.Value * Math.Cos(op1.ThetaRadians.Value) + op2.R.Value * Math.Cos(op2.ThetaRadians.Value); double newR = Math.Sqrt(newX * newX + newY * newY); double newTheta = Math.Atan2(newX, newY) * 180.0 / Math.PI; return new Vector(newR, newTheta); } catch { return new Vector(null, null); } } public static Vector operator -(Vector op1) { return new Vector(-op1.R, op1.Theta); } public static Vector operator -(Vector op1, Vector op2) { return op1 + (-op2); } public override string ToString() { string rString = R.HasValue ? R.ToString() : "null"; string thetaString = Theta.HasValue ? Theta.ToString() : "null"; return string.Format("({0}, {1})", rString, thetaString); }
} class Program {
public static void Main(string[] args) { Vector v1 = GetVector("vector1"); Vector v2 = GetVector("vector1"); Console.WriteLine("{0} + {1} = {2}", v1, v2, v1 + v2); Console.WriteLine("{0} - {1} = {2}", v1, v2, v1 - v2); Console.ReadKey(); } public static Vector GetVector(string name) { Console.WriteLine("Input {0} magnitude:", name); double? r = GetNullableDouble(); Console.WriteLine("Input {0} angle (in degrees):", name); double? theta = GetNullableDouble(); return new Vector(r, theta); } public static double? GetNullableDouble() { double? result; string userInput = Console.ReadLine(); try { result = double.Parse(userInput); } catch { result = null; } return result; }
}
</source>
Nulllable HasValue
<source lang="csharp">
using System; using System.Collections.Generic; using System.Text;
public class MyTest {
public static void NullableTest(Nullable<int> intVal1, int intVal2) { if (intVal1.HasValue == true) Console.WriteLine(intVal1); else Console.WriteLine("Value1 is NULL"); if (intVal2 > 0) Console.WriteLine(intVal2); else Console.WriteLine("Value2 is Null?"); }
}
</source>