Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi ,

May be that the question in not clear but i really need to understand.
i will explain

if i have class called Test contain 2 public int variable .
C#
class Test
{
public int x;
public int y;
}


if i declare a new object of Test class without initialize it using new keyward
like below
C#
class Main()
{
Test t;
t.x //appeared
t.y //appeared
}


why x and y are being accessed normally after dot operator ???
i know that they are public but my question is related with new keyward.
is there any purpose from showing them , they are useless because we don't use new keyward , right ??? so why visual studio show it in intellisense

another question : i need a link that describe clearly the difference between abstraction and encapsulation ,......there are many articles but i am confused and most of articles are confused with each other .
Posted

1 solution

Because int variables are Value types, not reference, and Value types do not need new.

A Reference variable "points" at the actual data, a Value variable is the actual data. So the declaration creates the whole space for the variable, instead of allocating enout space the the reference (or pointer).
This works for all Value Types, so if you declare a struct you will find the same thing:
C#
public struct MyStruct
    {
    public int i;
    public string s;
    }

C#
...
MyStruct ms;
ms.i = 66;
ms.s = "hello";
....
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900