How to determine what type means

Excerpt from Eric Lippert 's blog about What it means to "eat", is :

The general concept of types is that a type is a set of [...] values, and assignment compatibility simply checks if a given value is a member of the required set. But this is not the case in C #.

An example that he gives is that null is string returns false , but string b = null completely C # compiler.

This may be a stupid question, but what is the best way to define the idea of ​​a “type” in the context of C # .Net? Is it just a word used to define ... storage rules? ... in the CLR? I understand how bad (and terribly wrong) this definition is, but I try my best to fit a beautiful wrapper and lean around an idea like.

Note: the simpler, but also completely accurate, the better. (strong N type , here).

+5
source share
5 answers

What is the best way to define the idea of ​​a "type" in the context of C # .Net? Is it just a word used to define ... storage rules? I try my best to fit a good wrapper and get ideas around like.

This is a difficult problem. You have opened a link to my article on is , but I think you really want to read this:

http://blogs.msdn.com/b/ericlippert/archive/2011/08/29/what-is-this-thing-you-call-a-quot-type-quot-part-one.aspx

http://blogs.msdn.com/b/ericlippert/archive/2011/09/07/what-is-this-thing-you-call-a-quot-type-quot-part-two.aspx

In short:

From a “mathematical” point of view, a type is like a number: an abstract quantity that we can manipulate using rules. For example, if T is a type, then T[] also a type, "etc.

Once we have an abstract concept of type, we can assign each expression a type, and then we can make a verifier that automatically determines whether the program should follow type safety rules or not.

+10
source

The traditional English definition of Type is a good start.

a category of people or things that have common characteristics.

The .NET Type defines a specific category of .NET object (i.e.: System.IO.Stream). It contains a set of properties and methods (characteristics) of a certain type of .NET object.

Are you looking for a more technical description (e.g. memory management, etc.)?

+3
source

There are some good answers to a similar question: Why does the operator operator return false if set to null?

A more detailed discussion of the null literal is here: What is the type of the null literal?

It seems that earlier versions of .NET used the null type for symbolic purposes, but it was subsequently removed in later versions.

From the point of view of the IL programmer, I can say that when an instruction like if (myvar == null) 'is written in IL, it will look something like this:

 Ldloc myvar brfalse IfNullLabel 

A variable is checked for a null reference with only one IL statement, regardless of its type. If this were matched to another string, the Equals method is called. Therefore, the internal value is null when its reference points to a null literal. So simple.

+1
source

I think you can think about it a bit .. according to the Programming Guide

The information stored in the type may include the following:

  • The storage location that the type variable requires.
  • The maximum and minimum values ​​that it can represent.
  • The members (methods, fields, events, etc.) that it contains.
  • The main type that it inherits from.
  • The place where memory for variables will be allocated at runtime.
  • The types of operations that are allowed.

I know it doesn't take null into account, but in C # null there really is simply no memory reference address.

0
source

You will have a little more context to help answer your question (" [...] define the idea of 'type' in a C#.Net context[...] "), but here's a quick hit. The concept of type in .NET comes from object-oriented programming and is a generalization of data type from programming languages ​​to OOP. In your simplest form, you could say that ...

A type is a named template for a structure whose instances contain data and / or behavior.


Answers to questions in the EDIT section:

[...] how does the CLR handle type ideas?

I don’t know exactly what you mean by that, but the type in the CLR - the "named template" in my definition above - is physically stored as CIL and represented in the system as an object that itself (like all objects) has a type.

In this case, this type is called, of course, System.Type . (If you are interested, this type is physically stored in the mscorlib assembly.) I know that I use the term in my own definition, but this is difficult to do, since the concept is essentially recursive.

[...] does the CLR need to implicitly cast null to a string in the assignment string s = null? The answers so far do not mean no, this is not so, but what does this task allow? Is zero just considered a special case here?

Yes, this is a special case, and no, neither the compiler nor the CLR added null to string (or anything else). null not of type (1) and cannot be converted to any type - it is a special “value” that indicates the absence of value (2) and, therefore, is assigned to a variable of any reference type (3).



(1) Depending on where you look, the null type may or may not be.

(2) Such a value will be a reference for an instance of this type.

(3) There are two types of types - reference types and type values - and null true only apply to the previous view . It may appear in the source code (for example, int? i = null; ) as a nullable value assigned to a variable, but it is just syntactic sugar and what happens behind the scenes is very different and only concerns tangentially related to this discussion.

0
source

All Articles