Web Development

  Homes arrow Web Development arrow C# Variables
 Webmaster Tools
Base64 Encoding 
Browser Settings 
CSS Coder 
CSS Navigation Menu 
Datetime Converter 
DHTML Tooltip 
Dig Utility 
DNS Utility 
Dropdown Menu 
Fetch Content 
Fetch Header 
Floating Layer 
htaccess Generator 
HTML Encoder 
HTML Entities 
IP Convert 
Meta Tags 
Password Encryption
Password Strength
Pattern Extractor 
Ping Utility 
Pop-Up Window 
Regex Extractor 
Regex Match 
Scrollbar Color 
Source Viewer 
Syntax Highlighting 
URL Encoding 
Web Safe Colors 
Forums Sitemap 
Weekly Newsletter
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Contact Us 
Site Map 
Privacy Policy 
  >>> SIGN UP!  
  Lost Password? 

C# Variables
By: Developer Shed
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 14

    Table of Contents:

    Rate this Article: Poor Best 
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article




    C# Variables
    by Wrox Books


    Variables represent storage locations. Every variable has a type that determines the values to be stored in the variable. C# is a type-safe language and the C# compiler guarantees that values stored in variables are always of the appropriate type. The value of a variable can be changed through assignment or through use of the ++ and - operators.

    Variables are values that can change as much as needed during the execution of a program. One reason you need variables in a program is to hold the results of a calculation. Hence, variables are locations in memory in which values can be stored.

    Variable Declarations

    In C# all variables must be declared before they are used. In the declaration you must specify a data type and a name for the variable so that memory can be set aside for the variable. An optional initial value can be included with the declaration if you know what the value should be to start.

    Syntax for variable declaration

    [scope] [=initial value];


    The scope determines the accessibility of the variable (public, local, private) and the default is private. The data type specifies what kind of data the variable will hold. Optionally the variable can be initialised to a specific value. All variables must be initialised (given a value) before they are used in any calculation. If the variables are not initialised, the compilation of the program will fail.

    Value Type Variables

    Value type variables are also known as stack variables because they are stored on the stack. Value type variables can be directly declared and referenced. As the variables go out of scope, they are removed from the stack, ensuring the proper destruction of the variables. As the variables are created on the stack, they are not initialised; that is the responsibility of the program. The use of an uncapitalized variable will result in a compiler error.

    Example Value variable

    Int n; //uncapitalized int
    Long l = 327 //initialised long
    Float f = 3.13 F; //float initialised from single-precision literal

    Reference Type Variables

    Reference type variables are made up of two parts: the reference on the stack and the object on the heap. The creation of the object and the reference to the object is commonly known as the instantiation of the object.

    Example Reference variable

    To declare a reference-type variable, the syntax used are:

    string strMimico;city objToronto = null;object objGeneric;

    Example Object variable

    To create an object on the heap, we go through two steps, as follows:

    1. Declare the variable reference.
    2. Instantiate the object on the heap by calling the new operator.

    City objMimico; //declare objMimico to be a reference to a an
    // object of City type
    objMimico = new City(); //call the constructor of the City class to return
    // a reference that is stored in the objMimico reference

    The two lines can be combined into one:

    City objMimico = new City();

    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Web Development Articles
    More By Developer Shed



    - On Page SEO for New Domains
    - Improve Your Site`s Speed
    - Safari Books Online Review
    - Creating an Estore From the Ground Up
    - Most Common SEO Mistakes Developers Make
    - Making the Most of Your Titles and Meta Desc...
    - Five Ways Using Flash Can Damage Your Site
    - A Web Designer`s Guide to Colors
    - Use Webstarts to Create a Free Site
    - More Than Just Looks. How Your Web Design C...
    - How to Design Content Pages
    - Mint Review
    - Make Your WordPress Website Look Professional
    - How to Create a Mobile Web Site
    - Meta Tags: Still Useful?

    Developer Shed Affiliates


    © 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap