Tech Articles


NULL Handling in C# vs. PowerScript/SQL


C# handles NULL quite different than PowerScript and SQL.
Same code in PowerScript and C# may have different outcome.

 

 

Welcome to a set of examples showing you how C# handles NULL different than PowerScript.
In fact so different that same code can have completely different outcome.

 

Example 1 : Boolean Data Types

C# by default defines the Boolean data type without NULL.
You can opt-in NULL using the nullable operator (question mark).

  PowerScript C# C#
Data type Boolean bool bool?
Possible values true, false, null true, false true, false, null

 

Example 2 : Conditional Expressions

C# evaluates conditional expressions to its not-NULL Boolean type.
Conditional expressions in C# therefore often have different value than in PowerScript.
Adding null to the Boolean data type has no impact.

PowerScript Syntax Boolean result
  vs.   C# Syntax bool result
bool? result
result = (null > 42) null   result = (null > 42) false false
result = (null <> 42) null   result = (null != 42) true true
result = (null = null) null   result = (null == null) true true
result = (null <> null) null   result = (null != null) false false

 

Example 3 : Equality in IF, DO, and WHILE statements

Check for equality in IF, DO, and WHILE statements has different behavior in C# when comparing to NULL values.

 Name1   Name2  PowerScript C#
   
if (Name1 = Name2) then
   result = "NORTH"
else
   result = "South"
end if
if (Name1 == Name2)
   result = "NORTH";
else
   result = "South";

null null result = "South" result = "NORTH"
null "Mick" result = "South" result = "South"

 

Example 4 : Inequality in IF, DO, and WHILE statements

Similar differences exist when checking for inequality.

 

 Name1   Name2  PowerScript C#
   
if (Name1 <> Name2) then
   result = "NORTH"
else
   result = "South"
end if​
if (Name1 != Name2)
   result = "NORTH";
else
   result = "South";

null null result = "South" result = "South"
null "Mick" result = "South" result = "NORTH"

 

Example 5 : Check for NULL

Syntax for NULL is very different in C# though easy to read and type.

Check PowerScript C#
name IS NULL
if IsNull(name) then ...
if (name == null) ...
name IS NOT NULL
if not IsNull(name) then ...​
if (name != null) ...

 


Conclusion 

"Mind your step!" as you cross between PowerScript + SQL on one side and C# on the other side!

NULL handling is so different in C# that same code sometimes results in completely different outcomes.
Notice in DO and WHILE loops, difference may result in endless loops!

Comments (0)
There are no comments posted here yet

Find Articles by Tag

Debug Export JSON DLL Encoding GhostScript Automated Testing CI/CD Stored Procedure Performance REST External Functions Import UI Modernization Debugger PostgreSQL ODBC driver Array Expression COM DataType XML Database Table Schema Database Profile UI Themes Window OLE PowerBuilder Compiler DevOps Application ActiveX Open Source Data Database .NET Std Framework PowerBuilder (Appeon) OAuth 2.0 SnapDevelop Database Table WinAPI DataWindow JSON Error Sort Messagging PDFlib TLS/SSL Outlook Azure CoderObject Export Database Table Data Configuration PowerScript (PS) TFS RESTClient iOS PowerBuilder Database Object Graph HTTPClient Event Handling SVN Elevate Conference InfoMaker Import JSON RibbonBar JSONGenerator Jenkins DataWindow Event Handler Web Service Proxy Git Source Code NativePDF Windows 10 Icons Model .NET Assembly BLOB AI DragDrop Repository OAuth PFC Text PowerServer Web SQL API Authorization PBVM Event 32-bit PBDOM File JSONParser Visual Studio SOAP Icon Resize .NET DataStore OrcaScript Trial SDK UI Web API Android License Variable Windows OS Database Painter CrypterObject Encryption Menu Syntax Transaction Debugging Branch & Merge ODBC PBNI JSON Interface Source Control Migration Class SnapObjects Installation Charts Design PDF IDE Service RichTextEdit Control Authentication Script RibbonBar Builder Testing SqlModelMapper Database Connection SqlExecutor TreeView C# Oracle Linux OS Bug SQL Server TortoiseGit Validation WebBrowser 64-bit Platform Deployment Filter Excel PowerServer Mobile PostgreSQL MessageBox Mobile