KEMBAR78
Module 3 : using value type variables | PPTX
Using Value-Type
Variables
Overview
 Common Type System
 Naming Variables
 Using Built-in Data Types
 Creating User-Defined Data Types
 Converting Data Types
Common Type System
 Overview of CTS
 Comparing Value and Reference Types
 Comparing Built-in and User-Defined Value Types
 Simple Types
Overview of CTS
 CTS supports both value and reference types
Reference Type
Type
Value Type
Comparing Value and Reference
Types
 Value types:
 Directly contain their
data
 Each has its own copy
of data
 Operations on one
cannot affect another
 Reference types:
 Store references to their
data (known as objects)
 Two reference variables
can reference same object
 Operations on one can
affect another
Comparing Built-in and User-
Defined Value Types
 Examples of
built-in value types:
 int
 float
 Examples of user-defined
value types:
 enum
 struct
User-Defined
Value Types
Built-in Type
Simple Types
 Identified through reserved keywords
 int // Reserved keyword
- or -
 System.Int32
 Naming Variables
 Rules and Recommendations for Naming Variables
 C# Keywords
 Quiz: Can You Spot Disallowed Variable Names?
Rules and Recommendations for
Naming Variables
 Rules
 Use letters, the underscore,
and digits
 Recommendations
 Avoid using all
uppercase letters
 Avoid starting with
an underscore
 Avoid using abbreviations
 Use PascalCasing naming in
multiple-word names
different
Different
Answer42
42Answer



BADSTYLE
_poorstyle
BestStyle


Msg
Message

C# Keywords
 Keywords are reserved identifiers
 Do not use keywords as variable names
 Results in a compile-time error
 Avoid using keywords by changing their case sensitivity
abstract, base, bool, default, if, finally
int INT; // Poor style
Quiz: Can You Spot the
Disallowed Variable Names?
char $diskPrice;
char middleInitial;
int 12count;
float this;
2
3
4
1
int __identifier;5
Using Built-in Data Types
 Declaring Local Variables
 Assigning Values to Variables
 Compound Assignment
 Common Operators
 Increment and Decrement
 Operator Precedence
Declaring Local Variables
 Usually declared by data type and variable name:
 Possible to declare multiple variables in
one declaration:
--or--
int itemCount;
int itemCount, employeeNumber;
int itemCount,
employeeNumber;
Assigning Values to Variables
 Assign values to variables that are already declared:
 Initialize a variable when you declare it:
 You can also initialize character values:
int employeeNumber;
employeeNumber = 23;
int employeeNumber = 23;
char middleInitial = 'J';
Compound Assignment
 Adding a value to a variable is very common
 There is a convenient shorthand
 This shorthand works for all arithmetic operators
itemCount = itemCount + 40;
itemCount += 40;
itemCount -= 24;
Common Operators
Common Operators
• Equality operators
• Relational operators
• Conditional operators
• Increment operator
• Decrement operator
• Arithmetic operators
• Assignment operators
Example
== !=
< > <= >= is
&& || ?:
++
- -
+ - * / %
= *= /= %= += -= <<=
>>= &= ^= |=
Increment and Decrement
 Changing a value by one is very common
 There is a convenient shorthand
 This shorthand exists in two forms
itemCount += 1;
itemCount -= 1;
itemCount++;
itemCount--;
++itemCount;
--itemCount;
Operator Precedence
 Operator Precedence and Associativity
 Except for assignment operators, all binary operators are
left-associative
 Assignment operators and conditional operators are right-
associative
Creating User-Defined Data
Types
 Enumeration Types
 Structure Types
Enumeration Types
 Defining an Enumeration Type
 Using an Enumeration Type
 Displaying an Enumeration Variable
enum Color { Red, Green, Blue }
Color colorPalette = Color.Red;
Console.WriteLine(“{0}”, colorPalette); // Displays Red
Structure Types
 Defining a Structure Type
 Using a Structure Type
Employee companyEmployee;
companyEmployee.firstName = "Joe";
companyEmployee.age = 23;
public struct Employee
{
public string firstName;
public int age;
}
Converting Data Types
 Implicit Data Type Conversion
 Explicit Data Type Conversion
Implicit Data Type Conversion
 To Convert int to long:
 Implicit conversions cannot fail
 May lose precision, but not magnitude
using System;
class Test
{
static void Main( )
{
int intValue = 123;
long longValue = intValue;
Console.WriteLine("(long) {0} = {1}", intValue,
longValue);
}
}
Explicit Data Type Conversion
 To do explicit conversions, use a cast expression:
using System;
class Test
{
static void Main( )
{
long longValue = Int64.MaxValue;
int intValue = (int) longValue;
Console.WriteLine("(int) {0} = {1}", longValue,
intValue);
}
}
Review
 Common Type System
 Naming Variables
 Using Built-in Data Types
 Creating User-Defined Data Types
 Converting Data Types

Module 3 : using value type variables

  • 1.
  • 2.
    Overview  Common TypeSystem  Naming Variables  Using Built-in Data Types  Creating User-Defined Data Types  Converting Data Types
  • 3.
    Common Type System Overview of CTS  Comparing Value and Reference Types  Comparing Built-in and User-Defined Value Types  Simple Types
  • 4.
    Overview of CTS CTS supports both value and reference types Reference Type Type Value Type
  • 5.
    Comparing Value andReference Types  Value types:  Directly contain their data  Each has its own copy of data  Operations on one cannot affect another  Reference types:  Store references to their data (known as objects)  Two reference variables can reference same object  Operations on one can affect another
  • 6.
    Comparing Built-in andUser- Defined Value Types  Examples of built-in value types:  int  float  Examples of user-defined value types:  enum  struct User-Defined Value Types Built-in Type
  • 7.
    Simple Types  Identifiedthrough reserved keywords  int // Reserved keyword - or -  System.Int32
  • 8.
     Naming Variables Rules and Recommendations for Naming Variables  C# Keywords  Quiz: Can You Spot Disallowed Variable Names?
  • 9.
    Rules and Recommendationsfor Naming Variables  Rules  Use letters, the underscore, and digits  Recommendations  Avoid using all uppercase letters  Avoid starting with an underscore  Avoid using abbreviations  Use PascalCasing naming in multiple-word names different Different Answer42 42Answer    BADSTYLE _poorstyle BestStyle   Msg Message 
  • 10.
    C# Keywords  Keywordsare reserved identifiers  Do not use keywords as variable names  Results in a compile-time error  Avoid using keywords by changing their case sensitivity abstract, base, bool, default, if, finally int INT; // Poor style
  • 11.
    Quiz: Can YouSpot the Disallowed Variable Names? char $diskPrice; char middleInitial; int 12count; float this; 2 3 4 1 int __identifier;5
  • 12.
    Using Built-in DataTypes  Declaring Local Variables  Assigning Values to Variables  Compound Assignment  Common Operators  Increment and Decrement  Operator Precedence
  • 13.
    Declaring Local Variables Usually declared by data type and variable name:  Possible to declare multiple variables in one declaration: --or-- int itemCount; int itemCount, employeeNumber; int itemCount, employeeNumber;
  • 14.
    Assigning Values toVariables  Assign values to variables that are already declared:  Initialize a variable when you declare it:  You can also initialize character values: int employeeNumber; employeeNumber = 23; int employeeNumber = 23; char middleInitial = 'J';
  • 15.
    Compound Assignment  Addinga value to a variable is very common  There is a convenient shorthand  This shorthand works for all arithmetic operators itemCount = itemCount + 40; itemCount += 40; itemCount -= 24;
  • 16.
    Common Operators Common Operators •Equality operators • Relational operators • Conditional operators • Increment operator • Decrement operator • Arithmetic operators • Assignment operators Example == != < > <= >= is && || ?: ++ - - + - * / % = *= /= %= += -= <<= >>= &= ^= |=
  • 17.
    Increment and Decrement Changing a value by one is very common  There is a convenient shorthand  This shorthand exists in two forms itemCount += 1; itemCount -= 1; itemCount++; itemCount--; ++itemCount; --itemCount;
  • 18.
    Operator Precedence  OperatorPrecedence and Associativity  Except for assignment operators, all binary operators are left-associative  Assignment operators and conditional operators are right- associative
  • 19.
    Creating User-Defined Data Types Enumeration Types  Structure Types
  • 20.
    Enumeration Types  Definingan Enumeration Type  Using an Enumeration Type  Displaying an Enumeration Variable enum Color { Red, Green, Blue } Color colorPalette = Color.Red; Console.WriteLine(“{0}”, colorPalette); // Displays Red
  • 21.
    Structure Types  Defininga Structure Type  Using a Structure Type Employee companyEmployee; companyEmployee.firstName = "Joe"; companyEmployee.age = 23; public struct Employee { public string firstName; public int age; }
  • 22.
    Converting Data Types Implicit Data Type Conversion  Explicit Data Type Conversion
  • 23.
    Implicit Data TypeConversion  To Convert int to long:  Implicit conversions cannot fail  May lose precision, but not magnitude using System; class Test { static void Main( ) { int intValue = 123; long longValue = intValue; Console.WriteLine("(long) {0} = {1}", intValue, longValue); } }
  • 24.
    Explicit Data TypeConversion  To do explicit conversions, use a cast expression: using System; class Test { static void Main( ) { long longValue = Int64.MaxValue; int intValue = (int) longValue; Console.WriteLine("(int) {0} = {1}", longValue, intValue); } }
  • 25.
    Review  Common TypeSystem  Naming Variables  Using Built-in Data Types  Creating User-Defined Data Types  Converting Data Types