Basics

You must declare and initialize variables before using them to avoid an error. Failing to initialize a variable produces a runtime error.

TYPE THIS:

namespace DeclarelnitializeVariables

/// Summary description for Applylt.

public class ApplyVariableDeclaration

public void ApplyIt()

string sTest;

// Executing this line will give you the following error: Console.WriteLine (sTest);

RESULT:

Compile error = "Use of unassigned local variable 1sTest'

—H Between the curly braces, type two statements to declare and initialize a variable on separate lines and then press Enter.

—O Type a statement to declare and initialize a variable on the same line and press Enter.

—H Between the curly braces, type two statements to declare and initialize a variable on separate lines and then press Enter.

—O Type a statement to declare and initialize a variable on the same line and press Enter.

After you declare a variable, you initialize it by giving it a value. This value can come directly from the result of an operation by adding two numbers or concatenating a string, from the return of a method, or from the value of a property or field on an object.

When choosing a name for a variable, you need to give it a meaningful name. This lets you know what is in its storage without having to search through the code to find out what data type was used when it was declared. If you are storing someone's first name in a variable, you should give it a name like strFirstName. The convention used in strFirstName is Hungarian notation. Using this naming convention tells us the data type of the variable and the classification of the information (a first name).

.NET has a common set of data types that all .NET-compliant languages use. Having a Common Type System (CTS) is one of the foundations of the .NET platform that allows cross-language compatibility. The CTS is a formal specification that details how a type is defined. When you initialize a variable, you need to make sure that you pass data that can be stored in that type;otherwise a runtime error will occur.

INITIALIZE A VARIABLE

Untitled - Notepad

File Edit Foimat Help using System;

class DeclarelnltializeVariables

static void Mam()

string sStatement;

sStatement = "Make a statement";

Console.WriteLinefThe value for variables are: sStatement= + sStatement + "; ¡Count =" + ICount),|

—n Type the statement to declare and initialize a multiple variable of the same type on the same line and press Enter.

0 Add the

Console.Writeline function to write the values for the variables to the console screen.

File I Edit Format Help "1 Hew I rl r-J

Save in: J '-I" Documents

"3

I Installation Instructions I Installed Software ^My Pictures Z_] Shortcuts Zj Visual Studio Projects

D esktop

My Documents

D esktop

My Documents

"[C:\Csharp\DeclareImtiaMzeVanables.cs -

| Text Documents [\txt]

d

IANSI

< Click File O Save. —Q Type a name for the file. ^0 Click Save.

■ The source file saves to the directory and can now be compiled.

Note: You can save all of your Console applications in a specific directory (example: C:\CSharp).

Save Ctrl+S

Was this article helpful?

0 0

Post a comment