Basics

You need to know what data types are available to you in C# to properly store data. The following table outlines the intrinsic data types used by C#.

C# DATA TYPE

DESCRIPTION

SAMPLE CODE

Object

The ultimate base type of all other types

object o = null;

String

String type;a string is a sequence of Unicode characters

string s = "hello";

Sbyte (byte)

8-bit signed integral type (unsigned)

sbyte val = 12;

Short (ushort)

16-bit signed integral type (unsigned)

short val = 12;

int (uint)

32-bit signed integral type (unsigned)

int val = 12;

long (ulong)

64-bit signed integral type (unsigned)

long val1 = 12,-long val2 = 34L;

float (double)

Single-precision floating point type (double precision)

float val = 1.23F;

bool

Boolean type;either true or false

bool val1 = true;bool val2 = false;

char

Character type; a char value is a Unicode character

char val = 'h';

decimal

Precise decimal type with 28 significant digits

decimal val = 1.23M;

Microsoft Windows 2000 [Uersion 5.60.21951 iC> Copyright 1985 2000 Microsoft Corp.

C:\Documents and Sett ingsSfldmii

U:xusharpicsc Declare Initializi Microsoft CHÀ Visual CI Compila

Copyright <C> Microsoft Corp 2i

—0 Click Start O Run to open the Run dialog box.

—0 Click Start O Run to open the Run dialog box.

Microsoft Windows 2000 [Uersion 5.60.21951 iC> Copyright 1985 2000 Microsoft Corp.

C:\Documents and Sett ingsSfldmii

U:xusharpicsc Declare Initializi Microsoft CHÀ Visual CI Compila

Copyright <C> Microsoft Corp 2i stratorJcd C:\CSharp

0. All rights reserved.

Declare I nitialiseUariables.cs<12,7>: warning CS0219: The variable

' iOddNunbers' is assigned but its value is never used DeclareInitializeUariables.es<12,24>: warning CS0219: The variable 'iEvenHumbers' is assigned but its value is never used

—0 Change directories to where

Declarelnitialize Variables.cs is located by using the cd command.

—, Compile the class at the command prompt with the csc command.

Note: See page 36 for more information on compiling and running a file.

Run the program by typing the name of the executable file and pressing Enter.

-■ The program displays the message about the initialized variables.

CESS PROPERTIES

You can access the attributes of an object by using properties. A property is a member that provides access to an attribute of an object or a class. Examples of properties include the length of a string, the size of a font, the caption of a window, and the name of a customer.

Many objects in the .NET Framework have very useful properties. For example, you can use the DateTime object from the System class for a few handy properties. You can use the Now property to get a date/time stamp for the current date and time, or you can use the Today property to get the current date.

Accessing properties requires the class that defines the object to be available for use in your application. When using a .NET implicit object, you will need to make sure that the namespace for that class is imported. Next, you will qualify the class and the property that you want to access. You can do this by fully qualifying the class and its property (for example: System.DateTime.Now) or taking a shortcut that does not include the name of the base class that is referenced (for example: DateTime.Now).

^ACCESS^ROPERTIES

^ Untitled Notepad

^ACCESS^ROPERTIES

^ Untitled Notepad

D Open your text editor.

^0 Between the curly braces, create the Main function.

0 Declare variable to hold a Date Time string.

D Open your text editor.

—0 Type using System; to import the System namespace and press Enter.

0 Type the name of the class you want to create and press Enter.

^Q Type { }, placing the opening and closing curly braces on separate lines, to set off the body of the class.

^0 Between the curly braces, create the Main function.

0 Declare variable to hold a Date Time string.

Was this article helpful?

0 0

Post a comment