TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# ValueType Examples

Use the ValueType base class to understand how value types like int are represented.
ValueType. Indirection measures steps needed to load a value. Reference types add indirection. They point to another memory location. Value types do not. They are self-contained, often in the bytes on the evaluation stack.
First, this program shows an int value type. Please notice that no constructor is used on the int. It is stored entirely within stack memory space. The managed heap is not touched. No allocation occurs there.

Also: When a number is added to the int, that memory location's value is changed. No new int is created as a result of the addition.

Tip: Using value types, instead of reference types, is often a good optimization. There are counterexamples: large structs can be slower.

C# program that demonstrates value type using System; class Program { static void Main() { int value = 10; value += DateTime.Today.Day; Console.WriteLine(value); } } Output 15
Types. Value types are used constantly in C# programs. Even a simple for-loop uses an int value type to iterate. And floating-point values, such as doubles, can represent important (and large) numbers.

Structs: All values are considered structs. A struct is simply a region of bytes that can be accessed. These bytes represent values.

Struct
Class. The ValueType class is a base class for all value types, which include ints, shorts, doubles, and also structs such as DateTime instances. Next we use ValueType to refer to these values, and also as a parameter type in methods.

Info: ValueType is a class, which means it is not a value type itself. It is just a representation of a value type.

Class

And: Because it is the base class for values, you can refer to those values through a ValueType reference variable.

C# program that uses ValueType using System; class Program { static ValueType _valueType = false; static void Main() { // You can refer to System.Int32 as a ValueType. ValueType type1 = 5; Console.WriteLine(type1.GetType()); // You can refer to System.DateTime as a ValueType. ValueType type2 = DateTime.Now; Console.WriteLine(type2.GetType()); // You can refer to System.Boolean as a ValueType. Console.WriteLine(_valueType.GetType()); // Pass as parameter. Method(long.MaxValue); Method(short.MaxValue); } static void Method(ValueType type) { Console.WriteLine(type.GetType()); } } Output System.Int32 System.DateTime System.Boolean System.Int64 System.Int16
Next, what is the practical use of a ValueType parameter? It is a way to require that an argument is a value type. You can accept any argument with an object parameter, but it may be useful to further constrain the argument.

Object: This is the base class for all types. In this way it is similar to ValueType, but ValueType too is a subclass of object.

Object Type
Base class. The ValueType class may be useful in some narrow cases in actual programs. I have never needed it. But it is important as a base class for actual value types. This is why it has no constructor.

Usually: You should use the actual value types (such as int, long, short, DateTime) and not bother with ValueType itself.

DateTime
Blittable types. One concept that is related to value types is blittable types. A blittable type is compatible to an unmanaged type. Programs that use blittable types do not need to convert them before passing them to an unmanaged DLL.
In the book Expert .NET 2.0 IL Assembler we learn more about blittable types. In the .NET Framework some types, such as signed and unsigned integers, are blittable (page 371). They require no special conversion.

Bool: This type is sometimes not blittable. Some unmanaged systems require a four-byte boolean.

Char: This is also sometimes not blittable. It may need to be converted to an ANSI (1-byte) character.

Summary. A variable stores a value. In programs, even a reference (pointer) is a value. It is simply a value that points to another value. The ValueType class is an important detail: it serves as an abstract base class for all value types.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf