Infolink

 

Search This Blog

Nov 10, 2012

KEYWORDS,IDENTIFIERS and DATA TYPES in C#.Net

Introduction

KEYWORDS :
Keywords are the words which are language specific. A keyword is an essential part of a language definition. These words cannot be used as identifiers. Identifiers are nothing but names given to various entities used in a program. The examples of keywords are event, byte, break, using, class..etc

IDENTIFIERS :
Identifiers are program-defined tokens. Identifiers are names of various program elements like classes, methods, variables, namespaces, interfaces...etc. in the code that uniquely identify an element. Now let us see what these entities are.


A class contains some common properties and behaviors of an object. A method defines how an object behaves. Variables are placeholders for storing the values used in a program according to the type of data to be stored. Namespace help in organizing the classes so that they can be found easily. When two classes of are of same name, namespaces help in distinguishing these classes. That is different namespaces can be uesd to access these classes. Once a namespace is imported, we don't have to qualify items in that namespace by listing the entire namespace when we refer to them. An interface separates the definition of an objects from their implementation.
There are certain rules for C# identifiers :
  •     They can have alphabets,digits, and underscore characters
  •     They must not begin with digits
  •     Upper case and lower case letters are distinct because C# is a case sensitive language
  •     Keywords cannot be used as identifiers
DATA TYPES : C# is a strongly typed and rich language; therefore every variable in C# is associated with a Data type. Data types specify the type of values that can be stored in a variable. There are number of data types available in C# that allows a programmer to select the type appropriate to the requirement of the application.
The data types in C# are primarily categorized in three parts :
  1.     Value Types
  2.     Reference Types
  3.     Pointer Types



VALUE TYPES : Value types are of fixed length and stored on the stack in memory. When a value of a variable is assigned to another variable, that value is actually copied. This means that two identical copies of the value are available in the memory.
We can further categorize value types in two parts : User-defined types and Pre-defined types.

USER-DEFINED TYPES : The value types which are defined by the user is known as user-defined types. There are two user-defined types :
Structure : A structure is simply a composite data type consisting of a number of elements of other data types; it is almost same as classes but is used in context of relatively simple data types as it stores the value on the stack; normally, we refer structure as struct in C#.
Enumerations : An enumeration provides a way for attaching names to numbers in a program, which in turn increases the readability of code; we use enum keyword in C# to declare an enumeration.

PRE-DEFINED TYPES : Pre-defined types are also known as simple types. Pre-defined types are those value types which are already defined in C#. These types are available in C# as shown in below table.



Pre-defined types can be further sub-divided into three parts as shown below.
  1.     Numeric Types
  2.     Boolean Types
  3.     Character Types



NUMERIC TYPES : It is further divided into the following types:

Integral Types : Integral types hold whole numbers for example 123, -123, 3456, etc. The size and sign of the values depend on the integral data type we choose. Integral data type consists of two subtypes: Signed and Unsigned, each of which supports four types of integers. Signed integers support both positive and negative numbers while Unsigned integers holds only positive numbers.

Floating Point Types : In addition to whole number a floating point type can also hold a fractional part of a number such as -123.45 and 123.56. It can be further divided into two parts like float and double. To specify a number to be of float type, you must append the character f to the value such as 12.4f. If you omit f, the value will be treated as double. Here is the syntax for floating point variables:
            float percent = 67.4f;
            double val = 3.4;

Decimal Types : The decimal type is designed for financial and monitory calculations. To specify a number to be of decimal type, you must append the character M(or m) to the value such as 123.45M. If you omit M, the value will be treated as double. Here is the syntax for decimal type variables:

            decimal money = 123.45M;

CHARACTER TYPES : Character type(char) stores single character in memory. The char type assumes a size of two bytes but it holds only a single character. Here is the syntax for character type variables:

        char c = 'w';

BOOLEAN TYPES : Boolean type(bool) is used to test a particular condition during the executionof a program. Boolean type takes only two values: true and false. Here is the syntax for a boolean type variable:

        bool cond = true;

REFERENCE TYPES : Reference types are of varied length and stored on the heap in memory. When an assignment between two reference variables occurs, only the reference is copied and the actual value remains the same in memory location. We can further categorize reference types in two parts: User-defined types and Pre-defined types.

USER-DEFINED TYPES : User-defined reference types refer to those types which the user defines using pre-defined types. Following are the user defined reference types:

Classes : As we know that C# is purely an object oriented language so the base of all C# programs is the concept of class; classes provide the best approach to group together logically related items and functions that work on them.

Interfaces : Interfaces are used as superclasses whose properties are inherited by classes; the concept of an interface came into scene to support multiple inheritance(inheriting from more than one superclass) because in C# a class does not support multiple inheritance; extending the functionalities and behavior of one class into another class into another is called inheritance.

Delegates : Delegates are objects which point towards a method which matches its signature (values passed to the method) to that delegate; delegates are reference type used to encapsulate a method with a specific signature; in C++, this task could be performed by having pointer to a function.

Arrays : An Array is an ordered arrangement of data elements; in technical words, it contains a number of variables of the same type with the same name at contiguous locations of memory.

PRE-DEFINED TYPES : Pre-defined reference types are mainly of two types:

Object Type : The object type is the ultimate base type of all other pre-defined and user-defined data type in C#; you can use the object type to convert a value type on a disk to an object type to be placed on the heap.

String Type : String type is for creating and manipulating textual data in C#; you can perform various operations on strings such as copying, comparing, and concatenation on these string objects.

POINTER TYPES : Pointer types are used only in unsafe context. Pointer types do not inherit from the object base class which is an ultimate base class of .NET and we cannot convert pointer types to object type and vice-versa. Here is the syntax for declaring pointer type data types:
datatype* identifier;

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...