Search code examples
c#protocol-buffers

What's the difference between "Int32" and "google.protobuf.Int32Value"?


I'm writing a proto3 message on a legacy C# codebase, and I noticed that if I replace

import "google/protobuf/wrappers.proto";
import "google/protobuf/descriptor.proto";

message Foo {
    google.protobuf.Int32Value blah = 1;
}

with:

message Foo {
    int32 blah = 1;
}

the parser still works, but blah receives 0 instead of the proper value.

Why did it happen? Should int32 not be equivalent to google.protobuf.Int32Value?


Solution

  • Int32 is a built-in type that represents a 32-bit signed integer. It is defined in the System namespace, and you can use it like this:

    int x = 5;
    

    On the other hand, google.protobuf.Int32Value is a class defined in the Google Protocol Buffers library. This library provides a way to serialize structured data, such as messages or objects, in a compact binary format that is both efficient and easy to work with.

    Int32Value is a wrapper class that allows you to use a 32-bit integer value as a message field in a Protocol Buffers message. You would use it like this:

    google.protobuf.Int32Value x = 5;
    

    The main difference between Int32 and Int32Value is that the latter is a class, while the former is a built-in type. This means that Int32Value has additional functionality, such as the ability to be used as a field in a Protocol Buffers message, whereas Int32 does not have this capability.