0% found this document useful (0 votes)
103 views

A Simple C# Program.

This document provides information about C# programming including: 1. An example of a simple C# program that writes text to the console. 2. Instructions for compiling and running C# programs from the command line using csc.exe. 3. An overview of C#'s value types and reference types as well as details on numeric literals and primitive data types in C#.

Uploaded by

Mohammed Shukkur
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views

A Simple C# Program.

This document provides information about C# programming including: 1. An example of a simple C# program that writes text to the console. 2. Instructions for compiling and running C# programs from the command line using csc.exe. 3. An overview of C#'s value types and reference types as well as details on numeric literals and primitive data types in C#.

Uploaded by

Mohammed Shukkur
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

This is a simple C# program.

Call this program Example.cs.

using System; 
 
class MainClass { 
   
  public static void Main() { 
    Console.WriteLine("A simple C# program."); 
  } 
}

Using csc.exe, the C# Command-Line Compiler

To create and run programs using the C# command-line compiler:

1. Enter the program using a text editor.


2. Compile the program.
3. Run the program.

Compiling the Program

C:\>csc Example.cs

Prior to running csc.exe, you may need to run the batch file vcvars32.bat, which is typically
found in the //Program Files/Microsoft Visual Studio.NET/Vc7/Bin directory.

1. The name of a C# program is arbitrary


2. By convention, C# programs use the .cs file extension

The C# Keywords

abstract      as         base          bool         break
Byte          case       catch         char         checked
class         const      continue      decimal      default
delegate      do         double        else         enum
event         explicit   extern        false        finally
fixed         float      for           foreach      goto
if            implicit   in            int          interface
internal      is         lock          long         namespace
new           null       object        operator     out
override      params     private       protected    public
readonly      ref        return        sbyte        sealed
short         sizeof     stackalloc    static       string
struct        switch     this          throw        true
try           typeof     uint          ulong        unchecked
unsafe        ushort     using         virtual      volatile
void          while
C# has the following reserved keywords:

abstract              as                base                    bool           
      break

byte                  case              catch                   char           
      checked

class                 const             continue                decimal        
      default

delegate              do                double                  else           
      enum

event                 explicit          extern                  false          
      finally

fixed                 float             for                     foreach        
      goto

if                    Implicit          In                      Int            
      Interface

Internal              Is                lock                    long           
      namespace

new                   null              object                  operator       
      out

override              params            private                 protected      
      public

readonly              ref               return                  sbyte          
      sealed

short                 sizeof            stackalloc              static         
      string

struct                switch            this                    throw          
      true

try                   typeof            uint                    ulong          
      unchecked

unsafe                ushort            using                   virtual        
      void

volatile              while

Compile csharp source code in command line


csc /r:System.DLL /r:System.Windows.Forms.DLL /r:System.Drawing.DLL WinEvents.
cs
C#'s Value Types

C# contains two general categories of built-in data types:

1. value types
2. reference types.

C#'s reference types are defined by classes.

The C# Value Types

Type Meaning
bool Represents true/false values
byte 8-bit unsigned integer
char Character
decimal Numeric type for financial calculations
double Double-precision floating point
float Single-precision floating point
int Integer
long Long integer
sbyte 8-bit signed integer
short Short integer
uint An unsigned integer
ulong An unsigned long integer
ushort An unsigned short integer
Converting Numeric Strings to Their Internal Representation

For the numeric types, the .NET structure names and their C# keyword equivalents are shown
here:

.NET Structure Name C# Name


Decimal decimal
Double double
Single float
Int16 short
Int32 int
Int64 long
UInt16 ushort
UInt32 uint
UInt64 ulong
Byte byte
Sbyte sbyte

1. The structures are defined inside the System namespace.


2. The fully qualified name for Int32 is System.Int32.

Literals

literals refer to fixed values that are represented in their human-readable form.

1. To specify a long literal, append an l or an L. For example, 12 is an int, but 12L is a long.
2. To specify an unsigned integer value, append a u or U. Thus, 100 is an int, but 100U is a
uint.
3. To specify an unsigned, long integer, use ul or UL. For example, 984375UL is of type
ulong.
4. To specify a float literal, append an F or f to the constant. For example, 10.19F is of type
float.
5. To specify a decimal literal, follow its value with an m or M. For example, 9.95M is a
decimal literal.
6. A hexadecimal literal must begin with 0x (a zero followed by an x).
7. Primitives in C#

Type                Primitive                   Description                    
        Range
bool                System.Boolean              Boolean                        
        true or false

byte                System.Byte                 8-bit integer                  
        0 to 255

char                System.Char                 16
bit Unicode character               /u0000 to /uffff

decimal             System.Decimal              128-bit decimal                
        (+/-)1.0 � 10^-28 to (+/-)7.9 � 10^28, with 28 to 29 digits of precisi
on

double              System.Double               64-bit floating point          
        -1.79769313486232e308 to 1.79769313486232e308

float               System.Single               32-bit floating point          
        (+/-)1.5 � 10^-45 to (+/-)3.4 � 10^38, with 7 digits of precision

int                 System.Int32                32-bit unsigned integer        
        -2,147,483,648 to 2,147,483,647

long                System.Int64                64-bit integer                 
        -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

sbyte               System.SByte                8-bit integer                  
        -128 to 127

short               System.Int16                16-bit integer                 
        -32,768 to 32,767

string              System.String               not applicable                 
        String is an immutable variable length string.

uint                System.UInt32               32-bit unsigned integer        
        0 to 4,294,967,295

ulong               System.UInt64              64-bit unsigned integer         
       0 to 18,446,744,073,709,551,615

ushort              System.UInt16               16-bit unsigned integer        
        0 to 65,535

System Types and C# Shorthand

C# Shorthand                   CLS Compliant?                 System Type      
          Range                                   Meaning in Life

sbyte                          No                             System.SByte     
          128 to 127                              Signed 8-bit number

byte                           Yes                            System.Byte      
          0 to 255                                Unsigned 8-bit number

short                          Yes                            System.Int16     
          32,768 to 32,767                        Signed 16-bit number

ushort                         No                             System.UInt16    
          0 to 65,535                             Unsigned 16-bit number

int                            Yes                            System.Int32     
          2,147,483,648 to 2,147,483,647          Signed 32-bit number

uint                           No                             System.UInt32    
          0 to 4,294,967,295                      Unsigned 32-bit number

long                           Yes                            System.Int64     
          9,223,372,036,854,775,808 to 9,223,372,036,854,775,807            Si
gned 64-bit number

ulong                          No                             System.UInt64    
          0 to 18,446,744,073,709,551,615         Unsigned 64-bit number

char                           Yes                            System.Char      
          U0000 to Uffff                          A single 16-bit Unicode char
acter
float                          Yes                            System.Single    
          1.5 * 10^-45 to 3.4 * 10^38             32-bit floating point number

double                         Yes                            System.Double    
          5.0 * 10^-324 to 1.7 * 10^308           64-bit floating point number

bool                           Yes                            System.Boolean   
          true or false                           Represents truth or falsity

decimal                        Yes                            System.Decimal   
          10^0 to 10^28                           A 96-bit signed number

string                         Yes                            System.String    
          Limited by system memory                Represents a set of Unicode 
characters

object                         Yes                            System.Object    
          Any type can be stored in an object variable             The base cl
ass of all types in the .NET universe

You might also like