Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default value of an interface type

This may be a stupid question with an obvious answer I overlooked but if I have the following:

interface A { }
struct B {
    A some_field;
}

Then what would the default ctor of B get me as in how large would it be under the hood in bytes?

In other words (and from another perspective), what is the default value of a interface type and would it be considered a reference or value type since both classes (reference) and structs (value) can inherit from it?

like image 276
Jupiter Avatar asked Aug 16 '13 16:08

Jupiter


People also ask

Can an interface have a default value?

Since interfaces only work at compile-time, they cannot be used to set up runtime default values. Luckily, TypeScript provides a workaround for this. By using the TypeScript pick utility type, we can select properties from an interface and provide default values for them.

How do I give a default value to a TypeScript interface?

Normal way to set a default value to a property is the following. interface MyInterface { a: number; b: string; c: unknown; } const data1: MyInterface = { a: 0, b: "default-string", c: "something", }; const data2: MyInterface = { a: 0, b: "default-string", c: { value: 1 }, };

What is default value of interface in C#?

As I mentioned before, the C# syntax for an interface is extended to accept the following keywords: protected, internal, public and virtual. By default, the default interface methods are virtual unless the sealed or private modifier is used.

What is the default value for number in TypeScript?

To set a default value for a function parameter, use an equal sign right after the parameter name, e.g. function multiply(num: number, by = 10) {} . If a value for the parameter is not provided, the argument will be replaced with the default value.


2 Answers

Interfaces are reference types.

A field of an interface type will always be one pointer wide, and will default to null.

If you assign a struct that implements the interface, the struct will be boxed, and the field will contain a reference to the boxing object.

like image 179
SLaks Avatar answered Nov 09 '22 23:11

SLaks


As SLaks noted, a variable which is of an interface type will behave as a reference holder which is will be known known never to contain anything other than either null or a reference to object which implements the interface. As with all reference holders, the default value will be null.

A variable which is of a generic type which is constrained to an interface type (e.g. in class

Thing<T> where T:IComparable<T>
{T foo;
 ...}

field Foo will have the same default value as whatever the actual type substituted for T. For example, given

struct SimplePoint : IComparable<SimplePoint>
{public int x,y; 
 public int CompareTo(SimplePoint other)
   {...}
...}

then within Thing<SimplePoint>, the field Foo would have the default value (0,0).

Incidentally, it's worthwhile to note that while a conversion from a structure type to any reference type, including an interface it implements, will yield a reference to a new heap object containing a snapshot of the structure's fields, conversion of that reference to other reference types will yield a reference to the same heap instance.

like image 29
supercat Avatar answered Nov 10 '22 01:11

supercat