Scalar Data Types in Protobuf
Learn about the inbuilt data types available in Protobuf and how these get translated to the C# data type.
We'll cover the following...
In this lesson, we'll have a look at built-in data types available in Protobuf. We'll also learn how these get translated to the C# data type. We'll see how these basic data types work by adding them to the setup we've built previously. The code widget below contains the full setup that we have built up to this point, containing both the gRPC client and server applications.
syntax = "proto3"; option csharp_namespace = "BasicGrpcService"; package basic_grpc_service; service Chatbot { rpc SendMessage (ChatRequest) returns (ChatReply); } message ChatRequest { string name = 1; string message = 2; } message ChatReply { string message = 1; }
The inbuilt data types are also known as scalar value types. These are the primitive data types that are available in Protobuf without using any external libraries. Previously, we used string
, which is one such data type, which holds textual information. However, now we'll modify our Protobuf definition to include all other types. To do so, we'll go back to our BasicGrpcService
and BasicGrpcClient
projects in the code widget above. We'll then replace the content inside the chatbot.proto
file in both of these projects with the following, where the highlighted lines show the text that we have modified:
syntax = "proto3";option csharp_namespace = "BasicGrpcService";package basic_grpc_service;service Chatbot {rpc SendMessage (ChatRequest) returns (ChatReply);}message ChatRequest {string name = 1;string message = 2;}message ChatReply {string message = 1;bool answer_found = 2;bytes reply_in_bytes = 3;NumericPayload payload = 4;}message NumericPayload{float float_demo = 1;double double_demo = 2;int32 int32_demo = 3;int64 int64_demo = 4;uint32 uint32_demo = 5;uint64 uint64_demo = 6;sint32 sint32_demo = 7;sint64 sint64_demo = 8;fixed32 fixed32_demo = 9;fixed64 fixed64_demo = 10;sfixed32 sfixed32_demo = 11;sfixed64 sfixed64_demo = 12;}
We add some new fields to the ChatReply
message. The field on line 20 is of the type NumericPayload
, which itself is a message
type we define on line 23. This type was created solely to showcase all numeric types supported by Protobuf. Now, we'll go over the data types supported by Protobuf.
Scalar value types supported by Protobuf
Let's have a look at every scalar value type supported by Protobuf and see which C# types they get ...