Posted on

C# Namespace Guidelines

Let’s create some guidelines for documenting .Net Namespace.

For example, let’s look at the System.Collections Namespace. What do we notice?

Namespace – define the namespace using the following convention: System.Collections Namespace

Purpose – describe the purpose of the namespace, for example:

crone-park (33)

 

The System.Collections namespace contains interfaces and classes that define various collections of objects, such as lists, queues, bit arrays, hash tables and dictionaries.

Classes

We then identify the classes related to the namespace. For each, we identify it and describe its purpose. Use the correct case when documenting the
classes.

Note: it’s ArrayList, not Arraylist or arrayList.

Class Description

ArrayList – implements the IList interface using an array whose size is dynamically increased as required. Note how IList is spelt. Both the I for
Interface and L for List are capitalized.

BitArray – manages a compact array of bit values, which are represented as Booleans. True indicates that the bit is on (1). False indicates the bit is off
(0).

CaseInsensitiveComparer – compares two objects for equivalence, ignoring the case of strings.

CaseInsensitiveHashCodeProvider – Obsolete.

Structures

Next, identify the structure related to the namespace.

Structure Description

Dictionary Entry Defines a dictionary key/value pair that can be set or retrieved.

Interfaces

For interfaces, note the casing for IDictionaryEnumerator. I for Interface and D for Dictionary and E for Enumerator are capitalized.

Interface Description

ICollection – Defines size, enumerators, and synchronization methods for all nongeneric collections.

IComparer – Exposes a method that compares two objects.

IDictionary – Represents a nongeneric collection of key/value pairs.

IDictionaryEnumerator – Enumerates the elements of a nongeneric dictionary.

Use verb constructions to describe the purpose of each interface.

Posted on

What is a Statement in C#?

In C#, statements express the actions that a program takes.

Common actions include

  • Declaring variables
  • Assigning values
  • Calling methods
  • Looping through collections
  • Branching to another block of code

crone-park (35)

The order in which statements are executed in a program is called the

  • flow of control or
  • flow of execution.

This may vary every time that a program is run, depending on how the program reacts to input that it receives at run time.

A statement consist of one of the following:

  • Single line of code that ends in a semicolon or
  • Series of single-line statements in a block

A statement block is enclosed in {} brackets and can contain nested blocks.

 

 

Posted on

C#: how to write Hello World

The “Hello, World” program is traditionally used to introduce a programming language.

Here it is in C#:

using System;
class Hello
{
static void Main() {
Console.WriteLine(“Hello, World”);
}
}

crone-park (27)

What’s the file extension?
C# source files typically have the file extension .cs.

How to compile?
You can compile the program with the Microsoft C# compiler using the command line:
csc hello.cs

What does the Using directive do?

The “Hello, World” program starts with a using directive that references the System namespace.

What does a Namespace do?
Namespaces
Organize C# programs and libraries into a hierarchy.
Contain types and other namespaces.

For example?

The System namespace contains
a number of types, such as the Console class referenced in the program, and
a number of other namespaces, such as IO and Collections.

Benefits?
A using directive that references a namespace give you unqualified use of the types that are members of that namespace.

In the Hello World example, because we have the using directive, we can use Console.WriteLine as shorthand for System.Console.WriteLine.

The Main Member
The Hello class has a single member, the Main method.

The Main method is declared with the static modifier.
Note:
Instance methods can reference a particular enclosing object instance using the keyword this.

Static methods operate without reference to a particular object.

Where’s the entry point to C# program?
The static Main method serves as the entry point to the program.

What creates the output?

The output is produced by
the WriteLine method of
the Console class in
the System namespace.

Note: C# does not have a separate runtime library. Instead, the .NET Framework is the runtime library of C#.

Posted on

C# – What is a derived class?

As the name implies, a derived class originates from another class, which in C# is called a base class.

A derived class is a specialization of the base class. A derived class is created from an existing base class. It inherits the member variables and methods of the base class from which it is derived.

For example, if you have a base class Music, you might have a derived class Classical and another derived class StringQuartet.

While Classical derives from Music, and StringQuartet derives from Music, each derived class can represent different specializations of the base class Music.

crone-park (24)

What is a derived class?

When you define a class to derive from another class, the derived class gains all the members of the base class – except its constructors and destructors.

This means the derived class can reuse the code in the base class without re-implementing it.

In the derived class, you can add more members allowing it to extend the functionality of the base class.

  • Base Class – members are inherited by the derived class.
  • Derived Class – inherits those members from the base class.

Each Derived Class Has Only One Direct Base Class

A derived class can have only one direct base class.

However, inheritance is transitive.

If

StringQuartet is derived from Classical

Classical is derived from Music

StringQuartet inherits the members declared in Classical and Music.

Polymorphism and Derived Classes

Polymorphism let us treat objects of a derived class as objects of its base class.

For example, big cats (base class) hunt (a method) in different ways. A Lion (derived class) stalks, while a Cheetah (another derived class) outruns it.

Virtual Methods

A virtual method is a type of method which can be overridden in a derived class.

Tip: use override sealed if you want a virtual method in a base class to become sealed in a derived class.

MSDN Definition

A class that was created based on a previously existing class (i.e., base class). A derived class inherits all of the member variables and methods of the base class from which it is derived.

C+ Derived Class Example

class Derived : [virtual] [access-specifier] Base

{

// member list

};

class Derived : [virtual] [access-specifier] Base1,

[virtual] [access-specifier] Base2, . . .

{

// member list

};

 

Note: In C#, both inheritance and interface implementation are defined by the : operator, equivalent to extends and implements in Java. The base class should always be rightmost in the class declaration.

Single Inheritance

In “single inheritance,” a form of inheritance, classes have only one base class.

Consider the relationship illustrated in the following figure.

PrintedBook

Book

PaperbackBook

Note the progression from general (PrintedBook) to specific (PaperbackBook).

Kind of Relationships

The derived class has a “kind of” relationship with the base class.

Why?

A Book is a kind of a PrintedDocument

A PaperbackBook is a kind of a book.

Book is

both a derived class (from PrintedDocument) and a

base class (as PaperbackBook is derived from Book).

Code Sample

class PrintedDocument {};

// Book is derived from PrintedDocument.

class Book : public PrintedDocument {};

// PaperbackBook is derived from Book.

class PaperbackBook : public Book {};

Direct Base v Indirect Base

PrintedDocument is a “direct base” class to Book.

PrintedDocument is an “indirect base” class to PaperbackBook.

Why?

A direct base class appears in the base list of a class declaration – an indirect base does not.

The base class from which each class is derived is declared before the declaration of the derived class.

Learn more at http://msdn.microsoft.com