|
Hi,
Basic question to ask.
we know everything in .Net is derived from System.Object, even value and reference types.
So, how does CLR differentiate between the two and stores the value type on Stack and reference types on Heap?
In continuation to above, I have another query
we have a value type as
int age = 35;
and we have a class which contains both the value types and reference types as shown below
Class Company
{
int employeeCount = 3000;
Employee emp;
Public Company(Employee emp)
{
this.emp = emp;
}
}
and we created the object of Company class as shown below
Company cmpny = new Company(emp);
Question is where does the employeeCount will get stored(Stack or Heap) and Where does the cmpny gets stored?
Thanks in advance,
Praveen Raghuvanshi
Software Engineer,
India.
|
|
|
|
|
1- All the value types are derived from System.Valuetype and are stored in the stack. For other type of object needs to be initialized and are stored in the managed heap with all of its data members.
2- All the value type present in an object resides in managed heap together. They are not stored separately in the stack.
WWW, WCF, WWF, WPF, WFC .... WTF
|
|
|
|
|
Rags1512 wrote: we know everything in .Net is derived from System.Object, even value and reference types.
All value types are derived implicitly from the System.ValueType, which is derived from object.
Rags1512 wrote: Question is where does the employeeCount will get stored(Stack or Heap) and Where does the cmpny gets stored?
The answer is, where a value type is declares determines where it ends up. The cmpy object is stored in the heap, but with a pointer from the stack. employeeCount is stored in the heap memory, but is accessed [transparently] through a pointer on the stack.
Here is a good article on heap/stack allocation
http://www.c-sharpcorner.com/UploadFile/rmcochran/csharp_memory01122006130034PM/csharp_memory.aspx[^]
Though the author fudges the point about the cmpy pointer a little.
Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter.
Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.
|
|
|
|
|
Appreciate the explanation provided,
Praveen Raghuvanshi
Software Engineer,
India.
|
|
|
|
|
Rags1512 wrote: So, how does CLR differentiate between the two and stores the value type on Stack and reference types on Heap?
That value types are always on the stack and reference types are always on the heap is a common misconception. The distinction between value types and reference types is not where they are allocated (stack vs. heap), the difference is how they are passed to functions and assignment. Value types are passed by value. Reference types are passed by reference.
An article discussing this can be found here
Rags1512 wrote: we know everything in .Net is derived from System.Object, even value and reference types.
Also, not quite true. See this article for a good explanation.
|
|
|
|
|
Rags1512 wrote: will get stored(Stack or Heap)
We really don't care.
If you're interested in performance, don't use a managed system at all.
|
|
|
|
|
Do you want a simple answer or all the gory details[^]?
Simple:
Rags1512 wrote: how does CLR differentiate between the two
It's in the metadata, see .NET file format - Signatures under the hood, Part 1 of 2[^]
Rags1512 wrote: stores the value type on Stack and reference types on Heap
That's very misleading, reference types end up putting something in the heap as well as on the stack and value types may well be in the heap (as a field of a reference type, for example). The (well "a") difference is that an instance of a value type can be on the stack, whereas an instance of a reference type is always in the heap (but it may have a reference to it on the stack)
Rags1512 wrote:
Question is where does the employeeCount will get stored(Stack or Heap) and Where does the cmpny gets stored?
employeeCount will be in the heap, inside the instance of the Company it is part of. Where cmpny is stored is impossible to tell without more context. If that line is part of the declaration of a reference type (field with initializer) it will be in the heap as part of it's parent object, if that like is part of the declaration of a value type it may end up on the stack (or the instance of the value type it is in could be a field of a reference type), if that line is inside a method (local variable with initializer) it will definitely be on the stack. But the instance of the Company that cmpny refers to will always be in the heap.
I think.
|
|
|
|
|
|
It was helpful. Thanks!
Praveen Raghuvanshi
Software Engineer,
India.
|
|
|
|
|
check this link again
http://opexsolution.com/forum/viewtopic.php?f=18&t=8
you will get more clear idea about Reference and Value types
|
|
|
|
|
Good day.
I am building a small application and need to pass a startdate and enddate value to a SQL Stored Procedure. I have setup a DataGrid View with the stored Procedure as the Datasource. I have also created two textboxes in the form called Stardate and EndDate resprectively.
How do I pass the values typed into the textboxes to the SQL Query? I have thought of using a button to execute the query.
|
|
|
|
|
Pass the values to parameters of the stored procedure using code similar to the following:
con = new SqlConnection("server=xxx;database=xxx;uid=xxx;pwd=xxx");
cmd.Parameters.Add("@StartDate", SqlDbType.DateTime).Value = StartDate.Text;
cmd.Parameters.Add("@EndDate", SqlDbType.DateTime).Value = EndDate.Text;
cmd = new SqlCommand("insert_with_sproc", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
I'll leave data validation and any exception handling to you but that, essentially, is what you can do. There are, of course, many other ways to do this and you should select the method most appropriate to your needs.
me, me, me
"The dinosaurs became extinct because they didn't have a space program. And if we become extinct because we don't have a space program, it'll serve us right!"
Larry Niven
|
|
|
|
|
how can i edit,remove and add a node on a treeview in the runtime?
|
|
|
|
|
the following code snippet generate classes nodes has a childe nodes "Semester"
private void FillTreeNodes()
{
SysClass sysclass = new SysClass();
DataSet ds= sysclass.GetAllSysClasss();
TreeNode node;
foreach (DataRow row in ds.Tables[0].Rows)
{
node = new TreeNode();
node.Text = row["Class"].ToString();
node.Value = row["ClassId"].ToString();
node.NavigateUrl = "UpdateClass.aspx?classId="+node.Value;
GenerateSemesters(node,int.Parse(row["ClassId"].ToString()));
tree1.Nodes.Add(node);
}
}
private void GenerateSemesters(TreeNode node,int classid)
{
Semester semester=new Semester();
DataSet ds = semester.GetAllSemesters(classid);
TreeNode childenode;
foreach (DataRow row in ds.Tables[0].Rows)
{
childenode = new TreeNode();
childenode.Text = row["SemesterName"].ToString();
childenode.Value = row["SemesterId"].ToString();
childenode.NavigateUrl = "Updatesemester.aspx?semesterId=" + childenode.Value;
node.ChildNodes.Add(childenode);
}
}
i forget to minsion that is the best way for filling treeview is recursively !
modified on Monday, March 29, 2010 4:46 AM
|
|
|
|
|
|
Hi every one.
I'm working with MS Access2007-2010.As you know MS Access is too weak to make good program with it.For some reason i'm forced to work with MS Access.But it's too limited and i can't do what ever i want like C#.
I want to know is there any way to write c# code and use it in MS Access?
And use c# components in Access?
And imagin i have access form with some textBoxs,How can make the data encrypted and save it in Access dataBase?(From Access Forms,like the thing we do in C# Forms).
Thank you.
CanI
|
|
|
|
|
As far as I am aware you are stuck with the Access variant of VBA for your programming needs.
However, you should be able to create a dll (in c# or c++) and use that in the code (You'll have to look that bit up: it has been a lot of years since I used Access).
Roya Rayane wrote: MS Access is too weak to make good program with
That's really going to depend upon the requirement. If it's for an enterprise type solution then yes, you should be looking at something more robust like ORacle or SQl Server. However, for small, departmental requirements where there are few users, it might be just fine.
me, me, me
"The dinosaurs became extinct because they didn't have a space program. And if we become extinct because we don't have a space program, it'll serve us right!"
Larry Niven
|
|
|
|
|
You can write components in C#, exposing them through COM, which can be used in Access VBA. You cannot write C# code directly in Access and expect it to work.
|
|
|
|
|
How can i have something like this :
int i=0;
if(i>0)
List<string> Age = new List<string>();
else if(i==0)
List<int> Age = new List<int>();
string strAge = Age.ToString();
it gives this Error :
Error : The name 'Age' does not exist in the current context
Please give me a Solution.
modified on Monday, March 29, 2010 2:01 AM
|
|
|
|
|
Use like this.Age is not in if block thats why it not found.
List Age;
int i=0;
if(i>0)
Age = new List();
else if(i=0)
Age = new List();
string strAge = Age.ToString();
|
|
|
|
|
ummm ... isnt that the same as:
List Age = new List();
string strAge = Age.ToString();
because I is always going to be 0 in this case. Also if its not 0 it does the same thing as if it is 0 so ...
Brad Barnhill
|
|
|
|
|
How can i have something like this :
int i=0;
if(i>0)
List<string> Age = new List<string>();
else if(i==0)
List<int> Age = new List<int>();
string strAge = Age.ToString();
it gives this Error :
Error : The name 'Age' does not exist in the current context
Please give me a Solution.
|
|
|
|
|
You are attempting to create the List<>Age in the if statements and then working with Age outside the if statements, VS gets does not know if Age actually exists.
What if i = -1, neither of your ifs will create Age and it would be null so vs spits the dummy and makes you clean up your code. And as you are trying to make the list different formats then you are in trouble, if there is going to be characters in your Age then just use List<string> and test the values for numerics in the consuming process.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
|
I knew that But i want to do something like my question ?
|
|
|
|