1

The datatype int refers to System.Int32, which is a structure. I had few basic questions.

  1. How does it become a value type instead of a reference type.
  2. The structure is inheriting other interfaces and has methods implemented.

Can a structure inherit and can it hold implementation methods of the interfaces.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Muthukumar Palaniappan
  • 1,622
  • 5
  • 25
  • 49
  • 6
    A C# `struct` _is_ a [value type](https://msdn.microsoft.com/en-us/library/ah19swz4.aspx)... – Richard Ev Jul 28 '15 at 08:37
  • 1
    https://msdn.microsoft.com/en-us/library/saxz13w4.aspx – codeninja.sj Jul 28 '15 at 08:40
  • possible duplicate of [What's the difference between struct and class in .NET?](http://stackoverflow.com/questions/13049/whats-the-difference-between-struct-and-class-in-net) – Fabjan Jul 28 '15 at 08:42
  • @Fabjan This question has nothing to do with classes. – Yuval Itzchakov Jul 28 '15 at 08:43
  • @YuvalItzchakov this question has to do with structs and there is already **a lot** of information here on Stackoverflow about this matter. This question should be closed. – Fabjan Jul 28 '15 at 08:44
  • 1
    @Fabjan The fact that there is alot of searchable content still doesn't make this question a duplicate to what you consider to be a duplicate. – Yuval Itzchakov Jul 28 '15 at 08:46

3 Answers3

4

How does it become a value type instead of a reference type.

It becomes a value type as all other types in .NET do, by being declared as a struct, and not a class.

Can a structure inherit and can it hold implementation methods of the interfaces.

A struct cannot inherit, but can implement interfaces. int32 is a value type. Why cant they inherit? More on that in Why don't structs support inheritance?

By viewing the source, you can easily see that Int32 implements a bunch of interfaces:

public struct Int32 : IComparable, IFormattable, IConvertible,
                      IComparable<Int32>, IEquatable<Int32>
Community
  • 1
  • 1
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
2
  1. int and Int32 are the same thing, they are both value types int doesn't become a reference type.

  2. Stucts can implement interfaces

Mant101
  • 2,705
  • 1
  • 23
  • 27
2

As to your question int is just an alias that points to Int32 struct. The structure is some sort of lightweight object that is stored on stack whilst normal objects (classes) are stored on heap. Struct cannot inherit classes but only interfaces. It's also not possible to inherit from it. You can find a lot of information about structs here on StackOverflow or if you google for it.

Fabjan
  • 13,506
  • 4
  • 25
  • 52