Rapid Application Development Lec 1 2021
Rapid Application Development Lec 1 2021
Rapid Application Development Lec 1 2021
Development
C# Programming Constructs
Objectives
• After completing this topic, you will be able to:
• Explain how to declare variables and assign values.
• Use operators to construct expressions.
• Create and use arrays.
• Use decision statements.
• Use iteration statements
Declaring Variables and Assigning Values
Objectives
• After completing this lesson, you will be able to:
• Describe the purpose of variables.
• Describe the purpose of data types.
• Explain how to declare and assign variables.
• Explain how variable scope determines where a variable is accessible in an
application.
• Explain how to convert data in a variable to a different data type.
• Describe best practices for using read-only variables and constants.
What Are Variables?
• A variable represents a named location in memory for a piece of data.
• Variables store values required by the application in temporary
memory locations.
• A variable has the following six facets:
• Name. Unique identifier that refers to the variable in code.
• Address. Memory location of the variable.
• Data type. Type and size of data that the variable can store.
• Value. Value at the address of the variable.
• Scope. Defined areas of code that can access and use the variable.
• Lifetime. Period of time that a variable is valid and available for use.
What Are Data Types?
• A variable holds data that has specified type.
• C# is a type-safe language, which means that the compiler guarantees
that values that are stored in variables are always of the appropriate
type.
Commonly Used Data Types
Declaring and Assigning Variables
• Before you can use a variable, you must declare it so that you can specify its name and
characteristics.
• C# is case-sensitive.
• If you use the name MyData as the identifier of a variable, this is not the same as
myData.
• You can declare two variables at the same time called MyData and myData and C# will
not confuse them, although this is not good practice.
• Identifiers
• The name of a variable is referred to as an identifier.
• C# has specific rules concerning the identifiers that you can use:
• An identifier can only contain letters, digits, and underscore characters.
• An identifier must start with a letter or an underscore.
• An identifier for a variable should not be one of the keywords that C# reserves for its own use.
Declaring and Assigning Variables …
• When you declare a variable, you reserve some storage space for that
variable in memory.
• You must specify the type of data that it will hold.
• You can declare multiple variables in a single declaration by using the
comma separator; all variables declared in this way have the same
type.
• The syntax for declaring variables is shown in the following code
example.
DataType variableName;
// OR
DataType variableName1, variableName2;
Assigning a Value to a Variable
• After you declare a variable, you can assign a value to it for later use in the
application by using an assignment statement.
• You can change the value in a variable as many times as you want during the
application.
• The assignment operator (=) assigns a value to a variable.
• The syntax of a variable assignment is shown in the following code example.
variableName = value;
• The value on the right side of the expression is assigned to the variable on the left
side of the expression.
• The following code example declares an integer called price and assigns the number
10 to the integer.
int price = 10;
Assigning a Value to a Variable …
• The following code example assigns the number 20 to an existing integer variable called price.
price = 20;
• The type of the expression must match the type of the variable, otherwise your program will not
compile.
• For example, the code in the following code example will not work because you cannot assign a
string value to an integer variable.
int numberOfEmployees;
numberOfEmployees = "Hello";
• C# does not allow you to use an unassigned variable.
• You must assign a value to a variable before you can use it; otherwise, your program might not
compile.
Implicitly Typed Variables
• When you declare variables, you can also use the var keyword instead of
specifying an explicit data type such as int or string.
• When the compiler sees the var keyword, it uses the value that is assigned to
the variable to determine the type.
• Consequently, you must initialize a variable that is defined in this way when it
is defined, as shown in the following code example.
var price = 20;
• In this example, the price variable is an implicitly typed variable.
• Implicitly typed variables are useful when you do not know, or it is difficult to
establish explicitly, the type of an expression that you want to assign to a
variable.
What Is Variable Scope?
• The scope of a variable determines the parts of a program that can access that
variable.
• If you attempt to reference a variable outside its scope, the compiler will generate
an error.
• Levels of Scope
Variables can have one of the following levels of scope:
• Block
• Procedure
• Class
• Namespace
• These levels of scope progress from the narrowest (block) to the widest
(namespace).
Block Scope
• A block is a set of statements that is enclosed within initiating and terminating declaration
statements, such as a loop.
• If you declare a variable within a block, you
can use it only within that block.
• The lifetime of the variable is still that of the entire block.
• The following code example shows how to declare a local variable
called area with block-level scope.
}
Procedure Scope
• Variables that are declared within a procedure are not available outside that
procedure.
• Only the procedure that contains the declaration can use the variable.
When you declare variables in a block or procedure, they are known as local
variables.
• The following code example shows how to declare a local variable called
name with procedure-level scope.
void ShowName()
{
string name = "Bob";
MessageBox.Show("Hello " + name);
}
Class Scope
• If you want the lifetime of a local variable to extend beyond the lifetime of the
procedure, declare the variable at class-level scope.
• When you declare variables in a class or structure, but not inside a procedure, they are known as class variables.
• You can assign a scope to class variables by using an access modifier.
• The following code example shows how to declare a local variable called message with
class-level scope.