is there a way to model nullable arrays in protobuf?
I initially tried using
[...]
repeated google.protobuf.BoolValue values = 1;
[...]
but when used inside a gRPC service in C# this (obviously) translates to bool?[] instead of bool[]?.
Is there a way to achieve this?
Related
I consider this as a continuation of what I've learned from my two previous threads. Instead of Javascript I will be using pure C#.
I have a class with 3 parameters in it, and I am creating a variable which is a result of deserialization to class type
var param = js.Deserialize<ClassName>(jqData.Params);
Based on what I've learned from my first thread, it stores values based on inputs I've made within 3 textboxes that I have.
For our purposes, let's assume I only placed input in a second textbox out of three, so the values would be null, "abc", null.
Now, I got some very good suggestions from my second post, which I want to implement.
I want to create an array of objects, WITHOUT initializing, since those objects already hold values, reduce array down to 1 element based on criteria from that excellent post, and then proceed with my validation logic.
However, I am struggling with declaring array part. From what I saw here in SO, most of threads are talking about declaring and initializing those elements. I don't need it.
What I need is to declare an array, which would have class elements in it, something like array = [param.elem1, param.elem2, param.elem3], and when I run a code, it will return [null, "abc", null].
Can you please point me in the right direction on how to properly declare such array?
Your idea was close to how this can be handled. Just change your array = [param.elem1, param.elem2, param.elem3] to:
var myArray = new object[] { param.elem1, param.elem2, param.elem3 };
If you know the type of param.elem1/2/3, you can use the specific type (e.g. string[] instead of object[]).
I'm trying to learn how to access data in a System.Object. There's a Visual Studio 2015 c# .net application which calls a Matlab function using the following code:
object result = null;
matlab.Feval("matlabTest", 1, out result);
If I hover over the variable result when debugging it shows up as
result|{object[1]}. If I slide down to further expand result it shows up as [0]:{object[53,13]}. Then when sliding down further I can see the two dimensional array of strings and doubles. So, the debugger can see and display the valid data in "result" but I have no idea how to access the strings and doubles at runtime. Any suggestions would be appreciated.
Based on the information you provided, you need to do something like the following:
object[] first_array = (object[])result;
object[,] second_array = (object[,])first_array[0];
And then access the second array like this:
var item = second_array[5,5];
Another approach is to use the dynamic keyword like this:
dynamic dynamic_result = result;
var test_value = dynamic_result[0][5, 5];
You can always call <variable>.GetType() to see which type it is and then cast it based on this information.
an easy way to get access to object, is use typeof() to get it type, and use Reflection to access its data.
I want to convert NSMutableArray to CLLocationcoordinates2D[]
I tried the following but it gave me an error ,(this kind of type case is not allowed)
NSMutableArray* arrtest ;
// I had added some CLLocationcoordinates2D objects to this
CLLocationcoordinates2D[] locations = (CLLocationcoordinates2D[])arrtest
How could I convert?
If you really have an NSArray which you need to cast in a C# array, do the following:
var locations = NSArray.FromArray<CLLocationCoordinate2D>(arrtest);
However, considering that all Apple APIs in Xamarin.iOS return a C# array instead of an NSArray, you rarely need this. If you created the NSMutableArray yourself, I'd suggest to use a List< CLLocationCoordinate2D> instead.
One of the common programming best practices is "define variables as close to where they are used as possible".
I use structs frequently to create code thats almost self documenting in places. However, C# forces me to define the struct outside the method. This breaks the aforementioned best practice - its basically creating an unwanted global variable type for the entire class.
Is it possible to define a local struct inside a method, just like a local variable, and if not, could you give me a window into the reasons the C# designers decided to prevent this?
Use Case
I'm converting part of a spreadsheet into C# code. I'd like to use local structs within the method to store temporary information in an organized manner, without having to resort to hundreds of separate variables that are global in scope.
Update 2016-August: C# 7.0 may have this feature!
As of 2016-Aug, apparently, this will be a feature in C# 7.0.
So the C# compiler team agreed - wow!
Update 2020-July: Now supported by C# and C++
C++ has always fully supported this. And it's fantastic.
C# 7.0 now has value tuples for a lightweight data structure with named fields. See answer from Ghost4Man.
I believe it's not permitted to define named types within a method. As to why, I'll have to speculate. If a type is not going to be used outside, then its existence probably cannot be justified.
You can however define anonymous type variables within a method. It will somewhat resembles structures. A compromise.
public void SomeMethod ()
{
var anonymousTypeVar = new { x = 5, y = 10 };
}
It is a little late but this is my solution for lists - using anonymous vars as the structs inside of methods:
var list = new[] { new { sn = "a1", sd = "b1" } }.ToList(); // declaring structure
list.Clear(); // clearing dummy element
list.Add(new { sn="a", sd="b"}); // adding real element
foreach (var leaf in list) if (leaf.sn == "a") break; // using it
Anonymous elements (sn and sd) are somehow read only.
Since C# 7.0, you can use value tuples if you want a lightweight data structure with named fields. They can be used not only locally inside methods, but also in parameters, returns, properties, fields, etc. You can use local functions to somewhat emulate struct methods.
var book = (id: 65, pageCount: 535); // Initialization A
(int id, int pageCount) book2 = (44, 100); // Initialization B
Console.WriteLine($"Book {book.id} has {book.pageCount} pages.");
(int id, int pageCount) = book; // Deconstruction into variables
Console.WriteLine($"Book {id} has {pageCount} pages.");
Here book is of type System.ValueTuple<int, int> (a generic struct).
You could do something like this using anonymous types. MSDN examples below:
var v = new { Amount = 108, Message = "Hello" };
or
var productQuery =
from prod in products
select new { prod.Color, prod.Price };
foreach (var v in productQuery)
{
Console.WriteLine("Color={0}, Price={1}", v.Color, v.Price);
}
Nowadays, you could also use a named tuple: https://learn.microsoft.com/en-us/dotnet/csharp/tuples
No, this is not possible. If you are using .net 4.0, you could use Tuple<T1, ..., Tn> to replicate such a thing.
I don't see the reason why you would need such a struct - just use variables with speaking names and this shouldn't be any problem at all. In combination with explicit declaration using the class names there is very little space for ambiguity.
You can define an anonymous type within your method and use it. The anonymous type will be readonly, so it gets you the immutability that is desired of structs. It will not explicitly be a struct, but it will be fully defined and contained within your method.
var myLocalType = new
{
SomeValue = "Foo",
SomeId = 14
};
it's not a struct, but mayme a var can help you out here?
var person = new {Name= "John", City = "London"};
it's strong typed so it will be compile time checked
You can create a dynamic type in c# 4.0 to accomplish this task, but its not exactly what you are looking for.
However I believe that the maximum of defining variables as close to where they are used is meant to mean where a variable is introduced into program flow not where the type is declared. I believe that most types have some ability to be reused creating in method types limits you ability to create reusable blocks of code that operates on common data.
I've WebService written in Java that has method
#WebMethod
public void createUser(Long id, String name)
{
}
Now I want to use this method in .NET client. I think I should be able to use this method with nullable long but I can't. Is there any annotation or maybe some other way to force .NET use nullable types?
I think that if in java I use Long (not long) so it's natural that in .NET I'll have method with nullable parameter
In C#, nullable types use the '?' markup, so the datatype would be long?, or Nullable<Long>. If you need to get the actual long out of them, the best way to do it is NullableLong.Value
Thanks to Gratz' comment! What he means is, in a nullable type, you want to do NullableLong.HasValue, which returns a bool saying whether or not the variable is null.