0% found this document useful (0 votes)
47 views3 pages

C# in A Nutshell - Code Listings - 5

The document discusses C# language basics including syntax, types, operators, and statements. It provides code examples of using equality, comparison, logical, and conditional operators and explains short-circuiting behavior.

Uploaded by

mbsysde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views3 pages

C# in A Nutshell - Code Listings - 5

The document discusses C# language basics including syntax, types, operators, and statements. It provides code examples of using equality, comparison, logical, and conditional operators and explains short-circuiting behavior.

Uploaded by

mbsysde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

4/18/24, 1:15 AM C# in a Nutshell - Code Listings

Chapter 2 - C# Language Basics


A First C# Program

Syntax Basics

Type Basics

Numeric Types

Boolean Type and Operators


Equality and Comparison Operators

// == and != test for equality and inequality of any type, but always return a bool value
// (unless overloaded otherwise). Value types typically have a very simple notion of equality:

int x = 1;
int y = 2;
int z = 1;

Console.WriteLine (x == y); // False


Console.WriteLine (x != y); // True
Console.WriteLine (x == z); // True

Console.WriteLine (x < y); // True


Console.WriteLine (x >= z); // True

Equality with Reference Types

// For reference types, equality, by default, is based on reference, as opposed to the


// actual value of the underlying object (more on this in Chapter 6).

Dude d1 = new Dude ("John");


Dude d2 = new Dude ("John");
Console.WriteLine (d1 == d2); // False
Dude d3 = d1;
Console.WriteLine (d1 == d3); // True

public class Dude


{
public string Name;
public Dude (string n) { Name = n; }
}

And & Or Operators

// The && and || operators test for and and or conditions. They are frequently used in
// conjunction with the ! operator, which expresses not:

UseUmbrella (true, false, false).Dump(); // True


UseUmbrella (true, true, true).Dump(); // False

bool UseUmbrella (bool rainy, bool sunny, bool windy)


{
return !windy && (rainy || sunny);
}

Shortcircuiting

// The && and || operators short-circuit. This is essential in allowing expressions such as
// the following to run without throwing a NullReferenceException:

StringBuilder sb = null;

if (sb != null && sb.Length > 0)


Console.WriteLine ("sb has data");
else
Console.WriteLine ("sb is null or empty");

And & Or Operators - non-shortcircuiting

https://www.albahari.com/nutshell/E12-CH02.aspx 1/3
4/18/24, 1:15 AM C# in a Nutshell - Code Listings

// Same examples as before, but with & and | instead of && and ||.
// The results are identical, but without short-circuiting:

UseUmbrella (true, false, false).Dump(); // True


UseUmbrella (true, true, true).Dump(); // False

StringBuilder sb = null;

if (sb != null & sb.Length > 0) // Exception is thrown!


Console.WriteLine ("sb has data");
else
Console.WriteLine ("sb is null or empty");

bool UseUmbrella (bool rainy, bool sunny, bool windy)


{
return !windy & (rainy | sunny);
}

Conditional operator (ternary)

// The conditional operator (also called the ternary operator) has the form
// q ? a : b
// where if condition q is true, a is evaluated, else b is evaluated.

Max (2, 3).Dump();


Max (3, 2).Dump();

int Max (int a, int b)


{
return (a > b) ? a : b;
}

Strings and Characters

Arrays

Variables and Parameters

Expressions and Operators

Null Operators

Statements

Namespaces

C# 12
in a Nutshell
About the Book

Code Listings
C# 12 in a Nutshell
C# 10 in a Nutshell
C# 9.0 in a Nutshell
C# 8.0 in a Nutshell
C# 7.0 in a Nutshell

https://www.albahari.com/nutshell/E12-CH02.aspx 2/3
4/18/24, 1:15 AM C# in a Nutshell - Code Listings

Extras

Contact

Buy print or Kindle edition

Buy PDF edition

Read via O'Reilly subscription

https://www.albahari.com/nutshell/E12-CH02.aspx 3/3

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy