Visual C#

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Understanding Classes, Inheritance, and Namespaces

Classes

C# is an object-oriented programming language. One of the tasks of a C# developer is to


create user-defined types called classes. A class is a reference type that encapsulates data
(such as constants and fields) and defines its behaviors using programming constructs such as
methods, properties, constructors, and events.

A class represents an abstract idea that you would like to include in an application. For
example, the .NET Framework includes a Form class, which includes data fields for storing
information such as the size of the form, the form’s location, the form’s background color, title
bar text, and so on. The Form class also contains methods that define how a form behaves,
such as a Show() method that shows the form onscreen and an Activate() method that activates
the form by giving it the focus.

A class functions as the blueprint of a concept. When you want to work with a class in a
program, you create instances of the class, which are called objects. Objects are created from
the blueprint defined by the class, but they physically exist in the sense that they have memory
locations allocated to them and they respond to messages. For example, to create an actual
form in a program, you create an instance of the Form class. After you have that instance
available, you can actually work on it—you can set its properties and call methods on it.

Each object maintains its own copy of the data that is defined by the class. This allows
different instances of a class to have different data values. For example, if you have two
instances of the class Human— objYou and objMe—these two objects can each have a
different value for their EyeColor properties. You access the member of an object by using
ObjectName.MemberName syntax, where ObjectName is name of the class instance and
MemberName can be a field, a property, a method, or an event. When an object is created, it
creates its members in a special area in memory called the heap, and it stores a pointer to that
memory. Because classes use pointers to refer to their data, they are sometimes also called
reference types.

Static Members of a Class – A class can have static members (fields, methods, and so on).
Static members belong to the class itself rather than to a particular instance. A static class
cannot inherit from other classes. No instance of a class is required in order to access its static
members. When you access a static member of a class, you do so by prefixing its name with
the name of the class — for example, ClassName.StaticMemberName.

Access Modifiers – A class can define the accessibility of its member by including an access
modifier in its declaration. C# has four different access modifiers:

 public — Allows the member to be globally accessible.


 private — Limits the member’s access to only the containing type.
 protected — Limits the member’s access to the containing type and all classes derived
from the containing type.
 Internal — Limits the member’s access to within the current project.
Inheritance

Object-oriented programming languages such as C# provide a feature called


inheritance. Inheritance allows you to create new types that are based on types that already
exist. The original type is called a base class, and the inherited class is called a derived class.
When one class inherits from another class, the derived class gets all the functionality of the
base class. The derived class can also choose to extend the base class by introducing new data
and behavioral elements. In developing Windows forms, you will frequently inherit from the
Form class to create your own custom forms; these custom forms will be at least as functional
as an object of the Form class, even if you do not write any new code in the derived class. Value
types such as structs cannot be used for inheritance.

It is interesting to note that every single type (other than the Object class itself) that you
create or that is already defined in the framework is implicitly derived from the Object class of
the System namespace. This is the case to ensure that all classes provide a common minimum
functionality. Also note that a C# type can inherit from only a single parent class at a time.

Inheritance is widely used in the Framework Class Library (FCL), and you will come
across classes (for example, the Form class) that get their functionality from other classes (for
example, the Control class) as a result of a chain of inheritances.

Namespace

The .NET Framework provides a feature called a namespace that allows you to organize
classes hierarchically in logical groups based on what they do and where they originate. Not
only does a namespace organize classes, but it also helps avoid naming conflicts between
vendors because each classname is required to be unique only within its namespace. A general
convention is to create a namespace like this:

CompanyName.ApplicationName

In this case CompanyName is your unique company name and ApplicationName is a


unique application name within the company. All classes related to this application then belong
to this namespace.

A class is therefore identified, for example, as QueCertifications.Exam1.ExamQuestions,


where QueCertifications is the unique name for a company, Exam1 is a unique application
within that company, and ExamQuestions is the name of a specific class. QueCertifications
could have another class with the same name, ExamQuestions, as long as it belongs to a
different application, such as QueCertifications.Exam2. The objective of namespaces is to keep
the complete naming hierarchy unique so that there are no naming conflicts.

A namespace is a string in which dots help create a hierarchy. In the namespace


QueCertifications.Exam1, Exam1 is called a child namespace of QueCertifications. You could
organize classes at two levels here: at the level of QueCertifications and also at the level of
Exam1. You can create a hierarchy with as many levels as you want.

A System namespace in the FCL acts as the root namespace for all the fundamental and
base classes defined inside the FCL. One of the fundamental classes defined in the System
namespace is Object class (uniquely identified as System.Object). This class acts as the
ultimate base class for all other types in the .NET Framework.
The System.Windows.Forms namespace organizes classes for working with Windows
forms. The System.Drawing namespace organizes classes for creating graphical elements.

You might also like