0% found this document useful (0 votes)
27 views

C# in A Nutshell - Code Listings - 3

The document discusses C# language basics including defining variables of basic types like strings and integers, if/else conditional statements, classes and objects, and value types versus reference types. It also covers topics like namespaces, methods, and null values.

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)
27 views

C# in A Nutshell - Code Listings - 3

The document discusses C# language basics including defining variables of basic types like strings and integers, if/else conditional statements, classes and objects, and value types versus reference types. It also covers topics like namespaces, methods, and null values.

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/ 4

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
Predefined Type Examples

// string, int and bool types are examples of predefined types:

string message = "Hello world";


string upperMessage = message.ToUpper();
Console.WriteLine (upperMessage); // HELLO WORLD

int x = 2015;
message = message + x.ToString();
Console.WriteLine (message); // Hello world2015

bool simpleVar = false;


if (simpleVar)
Console.WriteLine ("This will not print");

int y = 5000;
bool lessThanAMile = y < 5280;
if (lessThanAMile)
Console.WriteLine ("This will print");

Custom Type Examples

// Just as we can build complex functions from simple functions, we can build complex types
// from primitive types. UnitConverter serves a a blueprint for unit conversions:

UnitConverter feetToInchesConverter = new UnitConverter (12);


UnitConverter milesToFeetConverter = new UnitConverter (5280);

Console.WriteLine (feetToInchesConverter.Convert (30)); // 360


Console.WriteLine (feetToInchesConverter.Convert (100)); // 1200
Console.WriteLine (feetToInchesConverter.Convert (milesToFeetConverter.Convert (1))); // 63360

public class UnitConverter


{
int ratio; // Field
public UnitConverter (int unitRatio) { ratio = unitRatio; } // Constructor
public int Convert (int unit) { return unit * ratio; } // Method
}

Instance vs Static Members

// The instance field Name pertains to an instance of a particular Panda,


// whereas Population pertains to the set of all Pandas:

Panda p1 = new Panda ("Pan Dee");


Panda p2 = new Panda ("Pan Dah");

Console.WriteLine (p1.Name); // Pan Dee


Console.WriteLine (p2.Name); // Pan Dah

Console.WriteLine (Panda.Population); // 2

public class Panda


{
public string Name; // Instance field
public static int Population; // Static field

public Panda (string n) // Constructor


{
Name = n; // Assign the instance field
Population = Population + 1; // Increment the static Population field
}
}

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

// The same code, but with Panda defined inside a namespace.

using Animals;

Panda p = new Panda ("Pan Dee");


Console.WriteLine (p.Name);

namespace Animals
{
public class Panda
{
public string Name;

public Panda (string n) // Constructor


{
Name = n; // Assign the instance field
}
}
}

Defining a Main method

// Here's our original program, without using top-level statements.


// (In LINQPad, we set the language in the toolbar above to 'C# Program'.)

using System;

class Program
{
static void Main() // Program entry point
{
int x = 12 * 30;
Console.WriteLine (x);
}
}

Conversions

// Implicit conversions are allowed when the compiler can guarantee they will
// always succeed and no information is lost in conversion:

int x = 12345; // int is a 32-bit integer


long y = x; // Implicit conversion to 64-bit integer

// In other cases, you need explicit conversions:

short z = (short)x; // Explicit conversion to 16-bit integer

x.Dump ("x");
y.Dump ("y");
z.Dump ("z");

Value Types

// The content of a value type variable or constant is simply a value.


// You can define a custom value type with the struct keyword:

Point p1 = new Point();


p1.X = 7;

Point p2 = p1; // Assignment causes copy

Console.WriteLine (p1.X); // 7
Console.WriteLine (p2.X); // 7

p1.X = 9; // Change p1.X

Console.WriteLine (p1.X); // 9
Console.WriteLine (p2.X); // 7

public struct Point { public int X, Y; }

Reference Types

// A reference type has two parts: an object and the reference to that object.

https://www.albahari.com/nutshell/E12-CH02.aspx 2/4
4/18/24, 1:15 AM C# in a Nutshell - Code Listings
Point p1 = new Point();

p1.X = 7;

Point p2 = p1; // Copies p1 *reference*

Console.WriteLine (p1.X); // 7
Console.WriteLine (p2.X); // 7

p1.X = 9; // Change p1.X

Console.WriteLine (p1.X); // 9
Console.WriteLine (p2.X); // 9

public class Point { public int X, Y; }

Null

// A reference can be assigned the literal null, indicating that the reference points to nothing:

Point p = null;
Console.WriteLine (p == null); // True

// The following line generates a runtime error (a NullReferenceException is thrown):


Console.WriteLine (p.X);

public class Point { public int X, Y; }

Nulls with structs

// A value type cannot ordinarily have a null value:

Point p = null; // This line will not compile.


int x = null; // Illegal, too.

public struct Point { public int X, Y; }

// See "Nullable Types" in Chapter 4 for a workaround.

Storage Overhead

// Structs take up as much room as their fields:

unsafe static void Main()


{
sizeof (Point).Dump(); // 8 bytes
sizeof (A).Dump(); // 16 bytes
}

struct Point
{
int x; // 4 bytes
int y; // 4 bytes
}

// However, the CLR requires that fields are offset within the type at an address
// that’s a multiple of their size:
struct A
{
byte b; // 1 byte
long l; // 8 bytes
}

Numeric Types

Boolean Type and Operators

Strings and Characters

Arrays

Variables and Parameters

Expressions and Operators

Null Operators

https://www.albahari.com/nutshell/E12-CH02.aspx 3/4
4/18/24, 1:15 AM C# in a Nutshell - Code Listings
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

Extras

Contact

Buy print or Kindle edition

Buy PDF edition

Read via O'Reilly subscription

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

You might also like