Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No default value for lists in .NET #3057

Merged
merged 3 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/dotnet/Examples/ErrorHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public async Task HandleErrors()
user = await app.LogInAsync(Credentials.Anonymous());
config = new PartitionSyncConfiguration("myPartition", user);
//:remove-start:
config.Schema = new[] { typeof(Examples.Models.User) };
config.Schema = new[] { typeof(Models.User) };
//:remove-end:
var realm = await Realm.GetInstanceAsync(config);
// :snippet-start:handle-errors
Expand All @@ -54,7 +54,7 @@ public async Task HandleErrors()
{
// See https://www.mongodb.com/docs/realm-sdks/dotnet/latest/reference/Realms.Sync.Exceptions.ErrorCode.html
// for a list of all error codes
case ErrorCode.RuntimeError:
case ErrorCode.BadQuery:
break;
}
};
Expand Down
5 changes: 4 additions & 1 deletion examples/dotnet/Examples/Objects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public partial class Person_Required : IRealmObject
// :snippet-start: default
// :replace-start: {
// "terms": {
// "PersonB": "Person" }
// "PersonB": "Person",
// "Person_Required": "PhoneNumber"}
// }
public partial class PersonB : IRealmObject
{
Expand All @@ -72,6 +73,8 @@ public partial class PersonB : IRealmObject
public ObjectId ID { get; set; }
//:remove-end:
public string Name { get; set; } = "foo";

public IList<Person_Required> PhoneNumbers { get; } = null!;
}
// :replace-end:
// :snippet-end:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{
// See https://www.mongodb.com/docs/realm-sdks/dotnet/latest/reference/Realms.Sync.Exceptions.ErrorCode.html
// for a list of all error codes
case ErrorCode.RuntimeError:
case ErrorCode.BadQuery:
break;
}
};
2 changes: 2 additions & 0 deletions source/examples/generated/dotnet/Objects.snippet.default.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
public partial class Person : IRealmObject
{
public string Name { get; set; } = "foo";

public IList<PhoneNumber> PhoneNumbers { get; } = null!;
}
9 changes: 6 additions & 3 deletions source/sdk/dotnet/model-data/define-object-model.txt
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ Default Field Values
~~~~~~~~~~~~~~~~~~~~

You can use the built-in language features to assign a default value to a property.
In C#, you can assign a default value in the property declaration.
In C#, you can assign a default value on primitives in the property declaration.
You cannot set a default value on a collection, except to set it to ``null!``.
Even if you set a collection to ``null!``, collections are always initialized on
first access, so will never be null.

.. literalinclude:: /examples/generated/dotnet/Objects.snippet.default.cs
:language: csharp
Expand Down Expand Up @@ -303,7 +306,7 @@ following rules:
- You must declare properties that are Realm object types as nullable.

- You cannot declare collections (list, sets, backlinks, and dictionaries) as
nullable, but their parameters may be:
nullable, but their parameters may be nullable according to the following rules:

- For all types of collections, if the parameters are primitives
(value- or reference-types), they can be required or nullable.
Expand Down Expand Up @@ -334,4 +337,4 @@ in your Realm objects. You can do so by setting ``realm.ignore_objects_nullabili
in a `global configuration file <https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/configuration-files>`__.

If you enable ``realm.ignore_objects_nullability``, nullability annotations
will be ignored on Realm object properties, including collections of Realm objects.
will be ignored on Realm object properties, including collections of Realm objects.