Instances and variables in C#

Asked

Viewed 327 times

-1

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, one int whole numbers and so on and so forth...

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

They can be classified as instances or objects?

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

1 answer

5


Yes, these described data are instances or objects, everything that is stored in memory during the 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 they all occupy memory space. Even a true (that in the background is a stored number 1) is an instance.

If this is not an instance, then what? Why would it be different? Always keep in mind what I have answered in previous question that objects are just the concreteness of a model, no matter what this model is, it 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 instances need not even be associated with variables, values can exist and, of course, be stored in memory without having a name linked to this memory address where the value is.

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

It is an instance, no matter if 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 place where the object is stored. See more in What’s the difference between Struct and Class?. Types by value usually (but not necessarily) are immutable and that means that any changes you try to make in it require you to create an instance. The reference types can be, in the case of string, but usually 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 that are exemplified in the question are like structs.

var i = 1;

generates an instance of the type int (has 4 bytes, one of which is the signal) and their 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;
}

It is now stored inside another object and will be stored in the heap since the object container is a class. At least so far, there are indications that in the future some classes may be allocated in the stack as optimization. In this case there is an instance of int which is within 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 kind. So:

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

And yet we can place an instance of a int in the heap:

object i = 1;

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

Write((int)i);

I put in the Github for future reference.

That’s a Unboxing, but is irrelevant to the question.

More:

  • Okay... let me see if I understand... an instance is to turn a model into something to use, for example: public class Example { int a public void Simple () { } } this is a model for creating instances, an instance being the model being used, such as private Example EX, EX is what will be used and Example is simply a model for creation, so instances are the models being used. Now I got it right?

  • That’s right. Note that the other answer clings to a wrong concept. Some people learn to program only object-oriented (which is a poorly defined concept, research right here) and OOP preaches that is only object what was created by a class, and only speaks in instances like this, then the people mix abstract concepts with concrete, ignore the actual implementation of languages and end up making mistakes, honest, but are still mistakes. To your question what you commented now, this example doesn’t matter, your question is on the basic types that don’t even have formally defined classes.

  • 1

    The easiest way to learn object orientation is nay use a language with these features. Recreate the idea OO using structs and functions in C makes it easier to be understood, in my opinion. Done this, it is easy to understand that the declaration of a simple class is basically a sugar syntax. In addition, the programmer learns).

  • @Gabriel perfect in everything!

  • @Gatti is a way of seeing yes. It’s not so simple, but it helps to understand. This model may be implicit.

  • Ok... instances are a model being used, variables are structures that can store certain instances, depending on their type (bool stores true or false, int stores integers, decimal decimal numbers etc...), so object-oriented programming consists of creating and using instances, I guess?

  • OOP is much more complex than this, and no, create and use instances you can in any paradigm.

  • But the part that instances are models being used and that variables can store instances is right, no?

  • Yes, I already said yes, in a simplified way that’s it. You need to understand the Stack Overflow better. This is a Q and A site, not a forum. You ask a question question and you get an answer, it’s not to be having an extensive discussion. After getting answers you can accept one of them to indicate to others that you have solved what you wanted to know. And you can vote on this and other answers, from the whole site, this helps sort the answers. You can learn a lot from reading the best posts website. And voting on them as it helps you.

  • Do you know any forums that talk about C# and respond quickly

  • 1

    Faster than here? No. Higher quality than here? No. What you need is to look for a structured way to learn, go concept by concept, really start from the beginning, from before the details you want to know, to build knowledge, not learn cake recipes. Trying to learn on forums doesn’t work, you can ask questions only. Here you have a lot of questions, learn to use the tools help too: https://answall.com/questions/tagged/c%23? Sort=votes&pageSize=50. Not ignoring what people say to you also helps.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.