Visual C#
Visual C#
Visual C#
Classes
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:
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
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.