Back to Writing
NOTESfirebasefirestorecsharpunity

Firestore: Type-Safe Document Conversion with ConvertTo<T>

February 22, 2021Updated Feb 17, 2026

Handling type conversion with Firestore's ConvertTo.

var docRef = db.Collection("users").Document("user1");
var snapshot = await docRef.GetSnapshotAsync();

if (snapshot.Exists) {
User user = snapshot.ConvertTo<User>();
Console.WriteLine($"User name: {user.Name}");
}

Custom class:

[FirestoreData]
public class User {
[FirestoreProperty]
public string Name { get; set; }

[FirestoreProperty]
public int Age { get; set; }
}

The ConvertTo method automatically converts Firestore document data into strongly-typed class instances.