SIGN UP MEMBER LOGIN:    
ARTICLE

Definite Assignment Rule in C#

Posted by Abhimanyu Kumar Vatsa Articles | C# Language May 27, 2011
An experimental comparison of "definite assignment" between the C/C++ and C# Languages.
Reader Level:

Introduction

In programming languages like C and C++, we can define a variable and then accidentally use it before initializing it. C and C++ languages have no in-built feature that can recognize that being done but C# does not allow us to use an unassigned variable; this rule is known as Definite Assignment Rule. We must assign a value to a variable before using it; otherwise we will get a "compile-time" error.

Consider in C++ Language

Let's take a look at an example given below in C++ Language; it has a main method having an unassigned variable named "sampleValue" but when we run this, no error is issued. Even this display some value say it as default value.

image002.jpg

Try assigning a value in the variable "sampleValue" and look at the output, it has changed (overwritten) by the new value. Look at the image below.

image004.jpg

Consider in C# Language

Let's take a look at the example given below in the C# Language; it has a main method having an unassigned variable named "price" but when we run this, a "compile-time error" is issued. Look at the image given below.

image006.jpg

Try to assign a value to the ariable "price" and look at the output, an error is issued. Look at the image below.

image008.jpg

So, finally at the end we have the following points.

  1. A variable must be definitely assigned at each location where it's value is obtained.
  2. A variable must be definitely assigned at each location where it is passed as a reference parameter.
  3. All output parameters of a function member must be definitely assigned at each location where the function member returns.
  4. The "this" variable of a "struct-type" instance constructor must be definitely assigned at each location where that instance constructor returns.

 HAVE A HAPPY CODING!!

Login to add your contents and source code to this article
share this article :
post comment
 

sam, i'm using TERBO.

Posted by Abhimanyu Kumar Vatsa May 27, 2011

If I compile the sample C++ code that declares sampleValue without initializing it, I get the warning message "warning C4700: uninitialized local variable 'sampleValue' used". I am using VS 2010 but VC has done that since before it was called visual; in other words, always. Also, I wonder how C# compares with VB .Net; I assume they are essentially the same but I wonder if VB 6 gave an error or warning about the problem.

Posted by Sam Hobbs May 27, 2011

thanks, its all because of you guys coz of the inspiration, motivation, guidelines i received. thanks for being here.

Posted by Abhimanyu Kumar Vatsa May 27, 2011

It is very nice seeing you comparing three languages . vert please to see screen short of all three languages. Very nice article. Thanks for Sharing

Posted by Dhananjay Kumar May 27, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor