Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I use the as keyword for a struct?

People also ask

Can this keyword be used in struct?

You can also use the this keyword in a struct and it will refer to the current instance of the struct. In the example below, the this keyword is used to refer to the fields of the current instance of the struct, to distinguish them from the input parameters that have the same name.

Why you not use an open keyword with structs?

So is there any need of usinge the keyword struct before declaring a structure object? If you don't use typedef or struct the compiler doesn't know that structname is a type. In C, you either need to use the struct keyword, or you need to typedef the struct type. In C++, the struct keyword is optional.

Does struct have this pointer?

The this pointer is a pointer accessible only within the nonstatic member functions of a class , struct , or union type. It points to the object for which the member function is called. Static member functions don't have a this pointer.

Do you need struct keyword C++?

Only C++ has added an extra rule that allows to omit the struct (and class ) keyword if there is no ambiguity. If there is ambiguity, also C++ requires the struct keyword in some places. A notorious example is stat on POSIX systems where there is a struct stat and a function stat .


Some of the existing answers aren't quite right. You can't use non-nullable types with as, because the result of as is the null value of the type if the first operand isn't actually of an appropriate type.

However, you can use as with value types... if they're nullable:

int a = 10;
object o = a;

int? x = o as int?; // x is a Nullable<int> with value 10
long? y = o as long?; // y is a Nullable<long> with the null value

So you could use:

Call? call = line.Tag as Call?;

Then you can use it as:

if (call != null)
{
    // Do stuff with call.Value
}

Two caveats though:

  • In my experience this is slower than just using is followed by a cast
  • You should seriously reconsider your current Call type:
    • It's exposing public fields, which is generally poor encapsulation
    • It's a mutable value type, which is almost certainly a mistake

I would strongly suggest you make it a class instead - at which point this problem goes away anyway.

Another thought: if the tag should always be a Call, then it's better to cast it:

Call call = (Call) line.Tag;

That way, if the data doesn't match your expectation (i.e. there's some bug such that the Tag isn't a Call) then you get to find out about it early, rather than after you've potentially done some other work. Note that this cast will behave differently depending on whether Call is a struct or a class, if Tag is null - you can cast a null value to a variable of a reference type (or a nullable value type), but not to a non-nullable value type.


A struct is a value type, so it cannot be used with the as operator. The as operator must be able to assign a value of null if the cast fails. This is only possible with a reference type or a nullable value type.

There are a couple ways to solve this, but your best bet is to change your Call type from a struct to a class. This will essentially change your type from a value type to a reference type, which allows the as operator to assign a value of null if the cast fails.

For more information on value types vs. reference types, this is a decent article. Also, have a look on MSDN:

  • value types
  • reference types
  • as-operator
  • nullable types.

From the C# Spec

§7.10.11 The as operator is used to explicitly convert a value to a given reference type or nullable type. Unlike a cast expression (§7.7.6), the as operator never throws an exception. Instead, if the indicated conversion is not possible, the resulting value is null.

References and nullable types can be null. Stucts are value types so they can't be null.


Call? call = line.Tag as Call?;