D. Edward Mitchell 16:00, 14 April 2020 (UTC) Hello World!    groupKOS Developer Share —usually UNDER CONSTRUCTION

Notes on CSharp

From groupKOS Developer Share
Jump to navigation Jump to search


Sites

DotNetPerls.com

1000 C# Programming examples

SanFoundry.com

TutorialsTeacher.com

C# Tutorials
URL: https://www.tutorialsteacher.com/csharp/csharp-tutorials

Topic

null coalescing operator (??)

What do two question marks together mean in C#?
he double-question mark is a 'null coalescing operator' which means "If whatever is to the left is not null, use that, otherwise use what's to the right."

Example:

// y = x, unless x is null, in which case y = -1.
int y = x ?? -1;

Associative arrays

With List<> data type:

Retrieved 14:35, 23 June 2020 (UTC) from https://www.dotnetperls.com/keyvaluepair

// Part 1: create a List of KeyValuePairs.
var list = new List<KeyValuePair<string, int>>();
list.Add(new KeyValuePair<string, int>("Cat", 1));
list.Add(new KeyValuePair<string, int>("Dog", 2));
list.Add(new KeyValuePair<string, int>("Rabbit", 4));

// Part 2: loop over list and print pairs.
foreach (var element in list)
{
    Console.WriteLine(element);
}

With Dictionary data type:

var article = new Dictionary<string, dynamic>();

article["title"] = "Article Title";
article["description"] = "Article Description";
article["total_pages"] = 15;

int pages = article["total_pages"];
string title = article["title"];

Retrieved 14:35, 23 June 2020 (UTC) from https://stackoverflow.com/questions/35130242/c-sharp-collection-or-array-with-string-keys-and-values-of-different-types

With an ExpandoObject:

dynamic article = new ExpandoObject();
article.Title = "Article Title";
article.Description = "Article Description";
article.TotalPages = 15;

int pages = article.TotalPages;
string title = article.Title;

Retrieved 14:35, 23 June 2020 (UTC) from https://stackoverflow.com/questions/35130242/c-sharp-collection-or-array-with-string-keys-and-values-of-different-types