Click here to Skip to main content
15,885,890 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
What visually, to the human eye, represents an instance (of a class type and struct type) in C# code?


C#
public class Owner
{
    // type members
}


What visually represents an instance (of a class type) in C# code: ???

C#
public struct Pet
{
    // type members
}


What visually represents an instance (of a struct type) in C# code: ???



For example, a literal value in C# code is visually represented by the following:
- 7 (an int literal)
- "Hello World" (a string literal)
- true (a bool literal)
- 'e' (a char literal)

What I have tried:

Please help with providing a C# code example or any helpful link for this issue. Thanks.
Posted
Updated 29-Nov-21 22:19pm
v3
Comments
Richard MacCutchan 30-Nov-21 5:39am    
When you create an instance of a class or struct the system allocates enough memory to contain all its members. So a visual representation is just like an array, a collection of bytes in memory.
BillWoodruff 30-Nov-21 8:42am    
If you study this book, your question will be answered.

https://www.amazon.com/Egyptian-Hieroglyphs-Complete-Beginners-Manley/dp/0500290288
PIEBALDconsult 30-Nov-21 9:23am    
Call ToString() on it and see what you get.

If you have a Pet - a cat, a dog, a stick insect - then you would be an Owner
So an instance of an Owner would be "Me", "You", "Him", "Her", "James", "Sarah", or ... anyone who might have a Pet.

A Pet would be the cat, dog, or stick insect that the specific Owner has: "My Cat", "Tiddles", "Bowser", "Sticky":

So I have a cat called Dij: I an an instance of the Owner class, Dij is an instance of the Pet class that has a relationship "Me.Pet = Dij":
C#
Pet dij = new Pet (Pet.Class.Cat, "Dij", Pet.Colour.Black + Pet.Colour.White);
Owner me = new Owner("OriginalGriff", Dij);


But be very careful when you start playing with structs: they are not the same thing as a class, not at all - and if you don't understand the difference, you can make some very big mistakes that are difficult to track down and fix. This may help: Using struct and class - what's that all about?[^]
 
Share this answer
 
Comments
OldBounty 30-Nov-21 3:19am    
In looking at the code you provided are you saying the expression --- new Pet(...) --- is what visually represents an instance of struct type Pet. While the expression --- new Owner(...) --- is what visually represents an instance class type Owner?

An expression represents an instance in C# code?
OriginalGriff 30-Nov-21 3:57am    
Not really: an instance is an "example of" - something that can be manipulated, that can have properties, that can do things (by having methods that can be called)

The code "new ClassName(optionalParameters)" isn't an expression per se - it's a way to create an instance. It causes space to be allocated for the specific instance and calls the class constructor for it so it's "prepped and ready to go" before the actual instance is returned to your code.

So
Owner me = new Owner("OriginalGriff", dij);
Does teh following:
1) Creates the space for the Owner instance of the heap.
2) Calls the two parameter constructor for the Owner class (passing the allocated space as this) and passes the "owner name" string and Pet instance reference to that constructor.
3) The constructor sets up the instance, saving the name, and Pet details for later.
4) The newly created Owner is returned to the caller, and stored in the variable called "me".

There is no "visual representation" of any of that as far as your user is concerned, unless your code specifically causes that to happen (and that would depend on the environment your code is running in - most classes are independent of the environment and shouldn't try to interact directly with a user).

Does that make sense?
OldBounty 30-Nov-21 14:20pm    
What you are explaining makes sense. But that is not what I mean.

What I mean is for example if you were to see 4, "Hello", true or 'i' in C# code, you would instantly say "hey that's a literal value".

But, in C# code, what represents an instance of a type? What exactly would it be that would instantly make you say “hey that an instance of a type”?
OriginalGriff 30-Nov-21 15:12pm    
Jake, did you read anything I said?
Richard MacCutchan 30-Nov-21 4:18am    
Guess who?
A visual representation of an instance of your class/struct would include whatever you think is appropriate as a string representation of that particular instance.

This is something you, as the developper, have to define; it really depends on the functionality of your class/struct. For a data structure, you could include some discrimminant choice of fields (e.g., Pet.Type, Pet.Name). For other classes, there is no general rule. Some classes do not even really need to have a custom string representation, the default one being sufficient.

In .NET, you can do that by overriding the ToString() method:
C#
public override string ToString()
{
   return "(whatever is appropriate as a string representation of the instance)";
}
 
Share this answer
 
v2
Comments
OldBounty 30-Nov-21 3:20am    
Seeing as how method ToString returns a string literal. A method invocation represents an instance in C# code?
Dave Kreskowiak 30-Nov-21 8:25am    
No, it doesn't. A method invocation is done on an object (a class instance.) It's not an instance itself. Methods may or may not return an instance of some class (an object.)

*
OldBounty 30-Nov-21 15:25pm    
Hey Dave "Methods may or may not return an instance of some class (an object.)" what you said here is what I was referring to.

To be more clear as to what I mean, for example if you were to see 4, "Hello", true or 'i' in C# code, you would instantly say "hey that's a literal value".

But, in C# code, what represents an instance of a type? What exactly would it be that would instantly make you say “hey that an instance of a type”?
Dave Kreskowiak 30-Nov-21 15:33pm    
Any variable. Everything is a type, even the literals. In your example, they are an integer, string, bool, and char. They are all types.
phil.o 30-Nov-21 14:12pm    
You can see the class definition as a blueprint: it defines what is an object, which are its properties and methods. An instance of that object is the result of building the blueprint.
 
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