Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Nullable arrays

I have a search function, but I would like LocationID to be an array of integers rather than just a single integer. I'm not sure how to do this since I want it to also be nullable. I've looked at doing int?[] but then I'd have to check the HasValue of every single entry. Is there a better way?

This is what I currently have:

public ActionResult Search(string? SearchString, int? LocationId,
    DateTime? StartDate,  DateTime? EndDate)
like image 550
Preston Avatar asked Apr 17 '13 20:04

Preston


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.


2 Answers

Arrays are always reference types, as is string - so they're already nullable. You only need to use (and only can use) Nullable<T> where T is a non-nullable value type.

So you probably want:

public ActionResult Search(string searchString, int[] locationIds,
                           DateTime? startDate,  DateTime? endDate)

Note that I've changed your parameter names to follow .NET naming conventions, and changed LocationId to locationIds to indicate that it's for multiple locations.

You might also want to consider changing the parameter type to IList<int> or even IEnumerable<int> to be more general, e.g.

public ActionResult Search(string searchString, IList<int> locationIds,
                           DateTime? startDate,  DateTime? endDate)

That way a caller could pass in a List<int> for example.

like image 116
Jon Skeet Avatar answered Oct 04 '22 21:10

Jon Skeet


Arrays are reference types, so you don't have to do anything, you already can pass null:

A method with the following signature can be called with all parameters as null:

public ActionResult Search(string SearchString, int[] LocationIds,
                           DateTime? StartDate, DateTime? EndDate)


foo.Search(null, null, null, null);

Please note: I additionally removed the question mark after string as it is a reference type as well.

like image 25
Daniel Hilgarth Avatar answered Oct 04 '22 22:10

Daniel Hilgarth