What's the best implementation of safe navigation operator

Is there an operator in C# that behaves like the safe navigation operator in groovy?

For instance, in groovy, doing something like this will prevent it from getting a NullPointerException if SessionData.CurrentSeminar is null.

int respId = SessionData.CurrentSeminar?.SeminCbaRespId;

How is this accomplished with C#?


That operator does not exist in C#. You could do it with an inline-if

int respId = SessionData.CurrentSeminar != null ? 
   SessionData.CurrentSeminar.SeminCbaRespId : default(int);

or as an extension method.

var respId = SessionData.CurrentSeminar.GetSeminCbaRespId();

public static int GetSeminCbaRespId(this typeofCurrentSeminar CurrentSeminar)
   return CurrentSeminar != null ? CurrentSeminar.SeminCbaRespId : default(int);

Need Your Help

Boolean Marshalling with LayoutKind.Explicit, Is this broken or failing as designed?

c# pinvoke boolean marshalling layoutkind.explicit

First of all the Boolean type is said to have a default marshal type of a four-byte value. So the following code works:

How to format Oracle SQL text-only select output

sql oracle formatting

I am using Oracle SQL (in SQLDeveloper, so I don't have access to SQLPLUS commands such as COLUMN) to execute a query that looks something like this: