Home > Articles > Programming > C#

This chapter is from the book

This chapter is from the book

C# Programming Elements

In the following sections we examine key elements of the C# language that we use throughout the book. From time to time, additional C# information is introduced, but the material in the following sections is used repeatedly.

Arrays

C# supports the same variety of arrays as C and C++, including both single and multidimensional arrays. This type of array is often referred to as a rectangular array, as opposed to a jagged array.

To declare a single-dimension integer array named myarray, the following C# syntax can be used:

int[] myarray = new int[12];

The array can then be initialized with 12 values using a for loop in the following manner:

for (int i = 0; i < myarray.Length; i++)
    myarray[i] = 2 * i;

The contents of the array can be written to the screen with a for loop and WriteLine() statement.

for (int i = 0; i < myarray.Length; i++)
    Console.WriteLine("myarray[{0}] = {1}", i, myarray[i]);

Note that i values will be substituted for the {0} and myarray[ ] values for {1} in the argument list provided with the WriteLine() statement.

Other array dimensions can follow the same pattern. For example, the syntax used for creating a two-dimensional array can take this form:

int[,] my2array = new int[12, 2];

The array can then be initialized with values using two for loops in the following manner:

for (int i = 0; i < 12; i++)
    for (int j = 0; j < 2; j++)
        my2array[i, j] = 2 * i;

The contents of the array can then be displayed on the console with the following syntax:

for (int i = 0; i < 12; i++)
    for (int j = 0; j < 2; j++)
        Console.WriteLine("my2array[{0}, {1}] = {2}",
                          i, j, my2array[i, j]);

Three-dimensional arrays can be handled with similar syntax using this form:

int[,,] my3array = new int[3, 6, 9];

In addition to handling multidimensional rectangular arrays, C# handles jagged arrays. A jagged array can be declared using the following syntax:

int[][] jagarray1;
int[][][] jagarray2;

For example, suppose a jagged array is declared as:

int[][] jagarray1 = new int[2][];

jagarray1[0] = new int[] {2, 4};
jagarray1[1] = new int[] {2, 4, 6, 8};

Here jagarray1 represents an array of int. The jagged appearance of the structure gives rise to the array's type name. The following line of code would print the value 6 to the screen:

Console.WriteLine(jagarray1[1][2]);

For practice, try to write the code necessary to print each array element to the screen.

Attributes, Events, Indexers, Properties, and Versioning

Many of the terms in this section are employed when developing applications for Windows. If you have worked with Visual Basic or the Microsoft Foundation Class (MFC) and C++ you are familiar with the terms attributes, events, and properties as they apply to controls. In the following sections, we generalize those definitions even more.

Attributes

C# attributes allow programmers to identify and program new kinds of declarative information. For example, public, private and protected are attributes that identify the accessibility of a method.

An element's attribute information can be returned at runtime using the NGWS runtime's reflection support.

Events

Events are used to allow classes to provide notifications about which clients can provide executable code. This code is in the form of event handlers. Again, if you have developed MFC C++ Windows code, you are already familiar with event handlers.

Here is code for a button-click event handler, extracted from a project developed later in this book:

private void button1_Click(object sender, System.EventArgs e)
{
    radius = Convert.ToDouble(textBox1.Text);
    textBox2.Text = (radius * radius * 22 / 7).ToString();
    textBox3.Text = (radius * 2.0 * 22 / 7).ToString();
}

The event handler contains code that will be executed when a button-click event occurs. The button is a button that resides on a form in a C# Windows application.

Indexers

Indexers are used by C# to expose array-like data structures, such as an array of strings. This data structure might be used by a C# Windows control, such as a CheckedListBox control.

    .
    .
    .
{
    private string[] items;
    public string this[int index] {
        get {
            return items[index];
        }
        set {
            items[index] = value;
            Repaint();
        }
}

The CheckedListBox class can then be altered with the following code:

CheckedListBox MyListBox;
MyListBox[0] = "List box title";
Console.Write(MyListBox[0]);

The array-like access provided by indexers is similar to the field-like access provided by properties.

Properties

A property is an attribute that is associated with a class or object. Windows controls offer a wide variety of changeable properties, including caption name, ID value, color, font, location, size, text, and so on.

Here is a small portion of a C# Windows program that modifies the properties of a button control:

this.button1.Location = new System.Drawing.Point(152, 192);
this.button1.Size = new System.Drawing.Size(176, 24);
this.button1.TabIndex = 6;
this.button1.Text = "Push to Calculate";
this.button1.AddOnClick(new System.EventHandler(button1_Click));

Properties can be read or written to as the need arises.

Versioning

C# supports versioning by addressing two levels of compatibility. The first is source compatibility, which occurs when code developed on an earlier version can be simply recompiled to work on a later version.

The second type of compatibility is binary compatibility, which occurs when code developed under an earlier version works under a newer version without recompiling.

Boxing, Unboxing, and the Unified Type System

All types in C# can be treated as objects. For example, the following line of code is acceptable in C#:

Console.WriteLine(12345.ToString());

In this case the ToString() method is used on the integer 12345 by treating it as an object.

An object box can be used when a value is to be converted to a reference type. This is called boxing. Unboxing is used to convert a reference type back to a value. For example:

int num1 = 12345;

object myobject = num1; // boxed

int num2 = (int) myobject; // unboxed

Here the integer number, 12345, is first converted to a reference type with the use of boxing, then converted from an object back to an integer value by casting the object (unboxing).

Classes, Structures, and Enum

C# provides simple, but unique, implementations to these common object-oriented features.

Classes

C# classes allow only single inheritance. Members of a class can include constants, constructors, destructors, events, indexers, methods, properties, and operators. Each member can, in turn, have a public, protected, internal, protected internal, or private access.

The makeup of a class is similar to that used in C and C++. For example:

public class Form1 : System.Windows.Forms.Form
{
    // variable declaration
    public double radius = 7.5;

    /// <summary> 
    /// Required designer variable
    /// </summary>
    private System.ComponentModel.Container components;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.TextBox textBox1;
     .
     .
     .

In this example, the class itself is public and contains a variable with public access. The designer variables, however, use a private qualifier to limit access. Classes use a pass by reference scheme as compared to a structures pass by value. For this reason, they tend to be faster than the equivalent structure.

Structures

Structures, as in C and C++, are very similar to classes. As a matter of fact, they can be created with members similar to those described for classes. Structures differ from classes in that they are value types with values being stored on the stack. This tends to make then slower than an equivalent class because passing by value is slower than passing by reference.

Point is typically used and implemented in C, C++, and C# as a structure:

struct Point
{
    public int x, y;
    public Point(int x, int y) {
 this.x = x;
 this.y = y;
 }
}

This example illustrates the typical syntax for creating a structure.

Enum

The enum type declaration is used to provide a type name for a group of symbolic constants that are usually related to one another. For example:

enum vehicle {
    Chrysler,
    Ford,
    GM
}

Use vehicle GM to access the GM element, and so on.

Namespaces

C# uses namespaces as an organization system applied both internally and externally. As a convention, developers usually name namespaces after the company they are developing code for.

The Visual C# AppWizard uses the following convention when creating a C# console code template:

using System;
namespace Tester
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class Class1
    {
        static void Main(string[] args)
        {
            //
            // TODO: Add code to start application here
            //
            int[] myint = new int[] {1,2,3,4,5};

            foreach (object o in myint) {
                Console.Write("the value of myint is: ");
                Console.WriteLine(o);
            }
        }
    }
}

We can modify that code to take on the following appearance:

using System;

namespace Nineveh_National_Research.CSharp.Tester
{
    /// <summary>
    /// Summary description for ForEachDemo.
    /// </summary>
    class ForEachDemo
    {
        static void Main(string[] args)
        {
            //
            // TODO: Add code to start application here
            //
            int[] myint = new int[] {1,2,3,4,5};

            foreach (object o in myint) {
                Console.Write("the value of myint is: ");
                Console.WriteLine(o);
            }
        }
    }
}

The namespace Nineveh_National_Research.CSharp.Tester is hierarchical. It really means that there is a namespace Nineveh_National_Research that contains a namespace named CSharp that itself contains a namespace named Tester.

The using directive can be used as a shorthand notation instead of writing out the whole namespace name. In the previous listing, the using directive allows all of the types in System to be used without qualification.

Predefined Types

In addition to the value and reference types discussed in the previous section, C# provides several predefined types.

For example, predefined value types include bool, byte, char, decimal, double, float, int, long, sbyte, short, uint, ulong and ushort. Table 1–1 lists and describes these types.

Table 1–1 C# Predefined Types

Type

Description

bool

Boolean type; true or false, 1 or 0

byte

Unsigned 8-bit integer

char

Unicode character

decimal

28-digit decimal type

double

Double precision real

float

Single precision real

int

Signed 32-bit integer

long

Signed 64-bit integer

object

Base type for all other C# types

sbyte

Signed 8-bit integer

short

Signed 16-bit integer

string

A sequence of Unicode characters

uint

Unsigned 32-bit integer

ulong

Unsigned 64-bit integer

ushort

Unsigned 16-bit integer


The types listed in the first column Table 1–1 are abbreviated versions of a longer structure name, but one preferred in C#.

Statements

Statement syntax in C# is basically the same as that for C and C++. In the following sections you'll see several familiar coding examples.

Blocks

C# allows blocking code so that one or more statements can be written in sequence. The following portion of code shows several blocks:

// block 1
Console.WriteLine("This is the first block");
{
    // block 2
    Console.WriteLine("This is the second block");
    {
        // block 3
        Console.WriteLine("This is the third block");
    }
}

Any number of blocks can be created using this format.

Miscellaneous Statements

C# provides a number of miscellaneous statements that are listed and briefly explained in Table 1–2.

Table 1–2 C# Miscellaneous Statements

Statement

Use

Break

For exiting an enclosing do, for, foreach, switch, or while statement.

Checked

Used to control the overflow checking context for arithmetic operations. All expressions are evaluated in a checked context.

Continue

For starting a new iteration of a do, for, foreach, switch, or while statement.

Lock

Used to obtain a mutual-exclusive lock for an object. With the lock in place, the statement will be executed then the lock will be released.

Return

Used to return control to the caller of the statement in which it appears.

Throw

Used to throw an exception.

Try

Used for catching exceptions while a block is executing.

Unchecked

Used to control the overflow checking context for arithmetic operations. All expressions are evaluated in an unchecked context.


You are already familiar with a number of these statements from your work with C and C++.

The do Statement

A do statement continues to execute a statement until the Boolean test is false. Here is a small portion of code:

int num1 = 0;

do {
    Console.WriteLine(num1);
    num1 += 2;
}
while (num1 != 20);

The output from this code will be the numbers 0 to 18. Every do statement will be executed at least one time with the Boolean test being made after the statement.

The Expression Statement

An expression statement evaluates a given expression and discards any value calculated in the process. Expressions such as (x + s), (y * 3), (t =2), and so on are not allowed as statements. The following is an example of an expression statement:

static int HereWeGo() {
    Console.WriteLine("We made it to HereWeGo");
    return 0;
}

static void Main(string[] args)
{
    //
    // TODO: Add code to start application here
    //

    HereWeGo();
}

Once again, the value returned by HereWeGo() is discarded.

The for Statement

The for statement, like its C and C++ counterparts, initializes the expression, and then executes an expression when the Boolean test is true. For example:

for (int i = 0; i < 10; i++) {
    Console.Write("the value of i is: ");
    Console.WriteLine(i);
}

This portion of code will report the value of i to the screen. The value of i increments from 0 to 9 before the Boolean condition is false.

The foreach Statement

The foreach statement is used to enumerate the contents of a collection. For example:

int[] myint = new int[] {1,2,3,4,5};

foreach (object o in myint) {
    Console.Write("the value of myint is: ");
    Console.WriteLine(o);
}

In this collection, each integer element will be reported to the screen. The collection, in general, can be any type.

The if and if-else Statements

The if statement executes based on a Boolean decision. If the statement is true, the expression will execute. If it is false, the statement will not execute. When used in conjunction with an else, the if-else combination will pass operation to the else when the if statement is false. For example:

int i = 2 * 23 / 12;

if ( i >= 5) 
    Console.WriteLine("This is a big number");
else
    Console.WriteLine("This is a reasonable number");

This portion writes one message or another based on the calculated value of the integer result.

The Label and goto Statements

The goto statement is used in conjunction with a label to transfer program control. For example:

goto C;

A: Console.WriteLine("This should be printed last");
return 0;

B: Console.WriteLine("This should be printed second");
goto A;

C: Console.WriteLine("This should be printed first");
goto B;

This concept is fairly straightforward. We recommend, however, limited use of goto statements.

The switch (case-break) Statement

C# switch statements, like those of C and C++, execute statements that are associated with the value of a particular expression. When no match occurs, a default condition is executed:

string str = "Top";

switch (str.Length) {
    case 0:
        Console.WriteLine("No characters in the string.");
        break;
    case 1:
        Console.WriteLine("One character in the string.");
        break;
    case 2:
        Console.WriteLine("Two characters in the string.");
        break;
    case 3:
        Console.WriteLine("Three characters in the string.");
        break;
    default:
        Console.WriteLine("A lot of characters in the string.");
        break;
}

A default option should always be provided in switch statements.

The while Statement

A while statement continues to execute while the Boolean result is true. For example:

int i = 5;

while (i <= 300) {
    i += 5;
    Console.WriteLine("Not there yet!");
}

The value of i is initialized to 5. When the final increment is made, the value in i will be 305, and thus the loop will stop executing. The while statement continues to execute until the value of i is equal to or exceeds 300.

Value and Reference Types

C# supports two main categories of types: value and reference types. You are already familiar with value types, including char, enum, float, int, struct, and so on. The key feature of the value type is that the variable actually contains the data

Reference types, on the other hand, include class, array, delegate, and interface types. An assignment to a reference type can affect other reference types derived from that reference type.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020