I'm porting my code to the new unified xamarin.ios api.
I have this as a parameter in a method.
nfloat fontSize = 0f
The compiler complains...
Error CS1750: Optional parameter expression of type `float' cannot be converted to parameter type `System.nfloat'
How do I write a literal for an optional parameter of nfloat?
You can't.
The native types in Xamarin.iOS/Xamarin.Mac (nfloat, nint, nuint) are not intrinsic managed types, and thus you can't create constants of those types.
Related
I found a piece of code like in the picture, in this case I know more or less what it causes, but generally I don't know the #( ) syntax.
What is the #( ) syntax and where can I find more about it?
DATA: lv_str TYPE string VALUE 'ABCD'.
DATA: dref1 TYPE REF TO data.
DATA: dref2 TYPE REF TO data.
* Old Syntax
GET REFERENCE OF LV_STR INTO dref1.
* New Syntax
dref2 = REF #( LV_STR ).
BREAK-POINT.
The # is a placeholder for the type of the variable.
You could write line 11 as this as well:
dref2 = REF data( lv_str ).
That would do the same thing. The # automatically takes the type of the variable on the left, if you don't specify it.
I've not seen it with TYPE REF TO before this, but it's fairly common as
VALUE #( )
I haven't found any documentation on the REF #( ) version but here is the official SAP documentation for VALUE #( ), it explains what the # does too.
The documentation of the Reference Operator REF states:
The # character for a data type that is determined by the following hierarchy:
If the data type required in an operand position is unique and known completely, the operand type is used.
If the operand type cannot be derived from the context, the data type of dobj is used.
If the data type of dobj is not known statically, the generic type data is used.
And also, I think that the "mother" documentation chapter is Constructor Operators for Constructor Expressions:
If the data type required in an operand position is unique and can be identified completely, the # character can be used instead of an explicit type specification type and the operand type is used.
If the operand type is not unique and is not known completely, if possible, a type inference is performed to determine a data type.
This is described in each constructor expression.
(NEW, VALUE, CONV, CORRESPONDING, CAST, REF, EXACT, REDUCE, FILTER, COND, SWITCH)
I am new to Generic Classes and Generic Methods. I have a simple cpode:
Func<string, string> selector = str => str.ToUpper();
string[] words = { "orange", "apple", "Article", "elephant" };
IEnumerable<String> aWords = words.Select(selector);
When I look at Select method it says:
IEnumerable<String> IEnumerable<String>.Select<String, String>(Func<string,string> collector)
How does Select generic method knows that String,String types are coming? Does it implicitly come from "selector" delegate? I am really confused.
Thanks
The C# compiler infers the type arguments for Select so that you don't have to type them. It burns those types into the compiled assembly.
Does it implicitly come from "selector" delegate?
Exactly. C# has some type inference so that you have to type less.
The Select method itself has no idea what piece of code called it. All it knows is that the type arguments are properly provided.
The C# MSDN documentation states that widening conversions are implicit and dont require an explicit cast. Accordingly, I find that the following code works without giving any errors:
public void MyMethod(int x)
{
float f = x; //widening conversion, works implicitly as expected.
...
}
But, the following does not seem to work, even though this also appears to me to fall under the category of a widening conversion.
public static void MyMethod(int x)
{
object o = x; // implicit conversion - works.
float f = (float)o; // implicit conversion expected here also - but doesnt work...
}
In the above second piece of code, I would expect an implicit conversion to happen from the int data stored in 'o' to the type specified in the explicit cast(float). But this doesnt happen and this code throws an InvalidCastException. Why is this so? I can understand an exception being thrown when 'o' is assigned to 'f' without any cast. But if a cast is specified explicitly and converting to that cast requires an implicit conversion (i.e. int to float) which is supported by the language, why is an exception thrown ?
Thanks.
casts do different things at different times. This line:
float f = (float)o;
Is not attempting to change the type of o - it's attempting to unbox a float. Unfortunately, you can only (within a few wiggles1) unbox the same type of value that was boxed - a boxed int has to be unboxed as an int.
You would instead have to do:
float f = (int)o;
Where the (int) is performing the unbox, and then the implicit conversion can occur from int to float, as per your first example.
For more, read Boxing and Unboxing:
Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type. When the CLR boxes a value type, it wraps the value inside a System.Object and stores it on the managed heap. Unboxing extracts the value type from the object. Boxing is implicit; unboxing is explicit...
1 There are some rules about Enums and their underlying type which I can't remember and won't ever deliberately use.
I understand that declaring variables of type Object or passing an Object variable as a parameter in a method is so that we can pass any object type e.g. Integer or String or an array. I just wanted to ask if we can also pass primitive data types or cast to integer primitive types too?
For example if I have a class Stack which allows us to push and pop objects of type Object, then i can use this class for Integer objects BUT can i use it for a primitive type int?
Yes you can, because Java will "auto-box" your primitive type. In other words, if you pass an int to your method, it will first get converted to an Integer, then that Integer will be passed as an argument to your method.
This tutorial gives more details about how it works.
I'm trying to convert come vb.net code into C# but came across optional in one of the functions.
Private Function doOpenConnection(ByRef cn As OleDb.OleDbConnection, ByRef cmd As OleDb.OleDbCommand, ByVal sConnString As String, Optional ByVal sUSP As String = "") As Boolean
It seems like instead of using overloading, VB.Net has an option to create it into one method/function. Does C# have a similar equilvalent or do I have to create a method for each possbility?
C# has an equivalent as of C# 4:
private bool doOpenConnection(ref OleDb.OleDbConnection cn,
ref OleDb.OleDbCommand cmd,
string sConnString,
string sUSP = "")
Note that you probably don't need ref for the first two parameters here - it's important that you understand how parameter passing works in C#.
C# 4 has both named arguments and optional parameters. See MSDN for more information. Note that there are various restrictions, in that optional parameters have to come before required ones (aside from parameter arrays), and the default value has to be a constant (or you can use the default(...) operator).
You can have optional parameters in C#.
From the MSDN:
Each optional parameter has a default value as part of its definition. If no argument is sent for that parameter, the default value is used. A default value must be one of the following types of expressions:
a constant expression;
an expression of the form new ValType(), where ValType is a value type, such as an enum or a struct;
an expression of the form default(ValType), where ValType is a value type.
Optional parameters are defined at the end of the parameter list, after any required parameters. If the caller provides an argument for any one of a succession of optional parameters, it must provide arguments for all preceding optional parameters. Comma-separated gaps in the argument list are not supported. For example, in the following code, instance method ExampleMethod is defined with one required and two optional parameters.
public void ExampleMethod(int required, string optionalstr = "default string",
int optionalint = 10)
{
Console.WriteLine("{0}: {1}, {2}, and {3}.", _name, required, optionalstr,
optionalint);
}