How to identity given data type is value type and reference type? [Resolved]

Posted by Allemahesh under C# on 8/19/2013 | Points: 10 | Views : 1132 | Status : [Member] [MVP] | Replies : 5
Can any one tell me if there is any simple way where I can identity whether a given data type is value type or reference type?




Responses

Posted by: Satyapriyanayak on: 8/19/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
http://www.codeproject.com/Articles/32029/Reference-and-Value-Types-in-C
http://msdn.microsoft.com/en-us/library/vstudio/ms173104.aspx
http://stackoverflow.com/questions/4598633/how-to-identify-each-parameter-type-in-a-c-sharp-method

If this post helps you mark it as answer
Thanks

Allemahesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 8/19/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
Public Class Form1


Dim x, y As Integer

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

x = 20

y = 30

MsgBox("Original value=" & x & " " & y)

change1(x)

change2(y)

MsgBox("Changed value=" & x & " " & y)

End Sub

Sub change1(ByVal x As Integer)

x = x + 10

End Sub

Sub change2(ByRef y As Integer)

y = y + 10

End Sub

End Class



Output

Original value= 20 30

Changed value= 20 40


If this post helps you mark it as answer
Thanks

Allemahesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 8/19/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
C# code


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Byval_And_Byref_in_csharp
{
public partial class Form1 : Form
{
int x;
int y;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
x = 20;
y = 30;
MessageBox.Show("Original value=" + x + " " + y);
change1(x);
change2(ref y);
MessageBox.Show("Changed value=" + x + " " + y);
}
public void change1(int x)
{
x = x + 10;
}
public void change2(ref int y)
{
y = y + 10;
}
}
}

Output

Original value= 20 30
Changed value= 20 40


If this post helps you mark it as answer
Thanks

Allemahesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 8/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Value Types:
-----------------------
Value type variables directly contain their values, which means that the memory is allocated inline in whatever context the variable is declared. There is no separate heap allocation or garbage collection overhead for value-type variables.
The built-in numeric types are structs, and they have properties and methods that you can access:
// Static method on type Byte.

byte b = Byte.MaxValue;


But you declare and assign values to them as if they were simple non-aggregate types:
byte num = 0xA;

int i = 5;
char c = 'Z';


Reference Types:
-----------------------
A type that is defined as a class, delegate, array, or interface is a reference type. At run time, when you declare a variable of a reference type, the variable contains the value null until you explicitly create an instance of the object by using the new operator, or assign it an object that has been created elsewhere by using new, as shown in the following example:
MyClass mc = new MyClass();

MyClass mc2 = mc;

All arrays are reference types, even if their elements are value types. Arrays implicitly derive from the Array class, but you declare and use them with the simplified syntax that is provided by C#, as shown in the following example:
// Declare and initialize an array of integers. 

int[] nums = { 1, 2, 3, 4, 5 };

// Access an instance property of System.Array.
int len = nums.Length;



Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Allemahesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ssj_Kumar on: 8/20/2013 [Member] Starter | Points: 25

Up
0
Down
Read the below article which will help you on clear understanding the Value type and reference type in detail
http://www.albahari.com/valuevsreftypes.aspx

after reading and understanding the above article you can easily say which is value type and reference type


Regards,
Jayakumar Selvakani

Allemahesh, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response