Instances and variables in C #

-2

In C # when you create a variable you create a value of a certain type, for example a bool can only receive true or false , a int integers and so on ...

I wanted to know how these values would be classified, true , false , 2 , 2.5f ...

Can they be classified as instances or objects?

Is an instance a value that is a copy of the original value with some changes?

    
asked by anonymous 24.10.2018 / 17:46

1 answer

3

Yes, these data are instantiated or objects, everything is stored in memory during execution is an instance of a type, is a concrete data based on some model that can be simple or complex, in these cases are very simple, are data that the processor recognizes, but all occupy memory space. even a true (which in the background is a stored number 1) is an instance.

If this is not an instance, then what? Why would it be any different? Always keep in mind what I've already answered in the previous question that objects are just the concreteness of a template , it does not matter which is this model, can be a very simple number of 1 byte or a few bytes that the processor understands, or something more complex that involves several parts.

And instance need not even be associated with variables , values may exist and, of course, be stored in memory without need to have a name linked to this memory address where the value is.

The instance is the object, and this alone, has nothing copy, original value, change, none of this.

An instance does not matter whether the type is by value or by reference. So these values are all objects (instances) equal to objects created based on classes, what changes is the form of access and eventually the location where the object is stored. See more at What's the difference between Struct and Class? . The types by value generally (but not necessarily) are immutable and this means that any change you try to make in it forces create an instance. The types by reference can be, if string , but generally are not. Also: Memory Allocation in C # - Value Types and Reference Types .

It may also be useful to see How a struct is organized in memory? . These types you are exemplifying in the question are like structs .

var i = 1;

it generates an instance of type int (it has 4 bytes, one of which is the signal) and its order depends on architecture) and it is stored in memory, in this case in the stack because it is a local variable.

class Exemplo {
    private int i = 1;
}

Now it is stored inside another object and will be stored in heap since the container object is a class. At least so far so, there are indications that soon some classes can be allocated in stack as optimization. In this case there is an instance of int that is inside an instance of Exemplo when it is created at some point in the code. All classes and structures created with C # code are composed of one or more instances of some type. So:

var e = new Exemplo();
Write(e.i); //acessando a instância de 'i' dentro da instância 'e'.

And we can still put an instance of a int in heap :

object i = 1;

In this case 1 is an integer value that will be stored inside an object of type object and that because it is a class it will be in heap . You can not access this value without a cast because object does not know how to access a value, it originally should not have any value. When cast is only accessing the instance already created, it does not create, convert or change anything:

Write((int)i);

This is an unboxing , but it is irrelevant to the question.

More:

24.10.2018 / 18:19