O'Reilly logo

C# in a Nutshell by Peter Drayton, Ted Neward, Ben Albahari

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Type Basics

A C# program is best understood in terms of three basic elements: functions, data, and types. This book assumes you have some programming experience, so let’s start with a brief overview of functions and data (which you should have some familiarity with) and then move on to explain types in more detail.

Functions

A function performs an action by executing a series of statements. For example, you may have a function that returns the distance between two points, or a function that calculates the average of an array of values. A function is a way of manipulating data.

Data

Data is values that functions operate on. For example, you may have data holding the coordinates of a point, or data holding an array of values. Data always has a particular type.

Types

A type has a set of data members and function members. The function members are used to manipulate the data members. The most common types are classes and structs, which provide a template for creating data; data is always an instance of a type.

Types are quite an abstract concept, so let’s look at two concrete examples.

The String Class

The string class specifies a sequence of characters. This means you can store values such as “.NET” or “http://oreilly.com”. You can also perform functions such as returning the character at a particular position on the string or getting its lowercase representation.

In this example, we output the lower case representation of “.NET” (which will be “.net”), and return the length of the string (which ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required