This question already has an answer here:
What does (datatype, datatype) x = (value, value) mean in C#? What is the actual data type of x?
(1 answer)
Closed 1 year ago.
i am still learning c# and .net core. Some time ago one guy send to me a exercise on which unfortunately i fall off mainly because of this function definition :
public static (int UserId, decimal MaximumTotalInstallment)[] Process(User[] users) {}
This is a library function which is used in XUnit test class in another namespace :
var expected = new[] {(1, 100m), (2, 1000m)};
// when
var output = BatchProcessingLibraryClass.Process(input);
// then
output.Should().BeEquivalentTo(expected);
Could You please tell me what this call of lib function exactly do ? I dont meet this kind of function declaration before.
You cannot tell what it does from the name (other than "process"), but you can tell what inputs are expected and what outputs are produced
Inputs - An array of User type called users.
Output - An array of a tuple. This tuple has two fields an integer named UserId and a decimal named MaximumTotalInstallment. This type is equivalent to ValueTuple<int,decimal>.
Other notes, the method is static which means it is called not for a particular instance of a class, but callable (like a library) from anywhere else.
Related
This question already has answers here:
C# Is it possible to pass a type into a method and have the method return an object of that type?
(7 answers)
Closed 2 years ago.
Ok, so this kinda goes over my head a little. also, I don't even know if it is possible to do so
I really don't even know the terms for a lot of this
First off this is for space engineers a game I play that has open c# script to interact with the game and I have found it very fun and a great way to learn some basics of code
Now what I'm trying to do is make a method, I think that is what it is called its this "internal List<> GetBlocksWithName", and what this method would do is search all blocks on a ship and then convert it to the right type then return a list of that type
but my issue is that I want to pass the 'type' I want to convert to at the end. not only if I try to call the method does it tell me that it's not valid. when I try to use the type that is passed through in the method as a type it says it's not a type
also, I'm not sure how to send the list back if its a different type each time
here is the code I'm using
internal void BlockScan()
{
GetBlocksWithName(IMyMotorAdvancedStator, "SolarHing"); // <- compile error IMyMotorAdvancedStator ('IMyMotorAdvancedStator' is a type, witch is not vaild in the give context)
return;
}
internal List<> GetBlocksWithName(Type blockType,string nameOfBlocks) // <- compile error List<>(Unexpected use of an unbound generic name) its because of an unfinshed return not sure how to specife the type if it will be different every time
{
List<IMyTerminalBlock> temp = new List<IMyTerminalBlock>();
GridTerminalSystem.SearchBlocksOfName(nameOfBlocks, temp);
return temp.ConvertAll(x => (blockType)x); // <- compile error blockType ('blockType' is a varible but is used like a type)
}
I know that there is a lot of stuff in there that is very specific to space engineers script but if someone could help me out would be of great help thanks in advance
Try the following
internal void BlockScan()
{
GetBlocksWithName<IMyMotorAdvancedStator>("SolarHing");
return;
}
internal List<T> GetBlocksWithName<T>(string nameOfBlocks)
{
List<IMyTerminalBlock> temp = new List<IMyTerminalBlock>();
GridTerminalSystem.SearchBlocksOfName(nameOfBlocks, temp);
return temp.ConvertAll(x => (T)x);
}
You pass in the Type = T as a generic, and use the Generic T to convert your variable x to the correct type.
Have a look at Generic programming in the documentation. To get a better understanding.
This question already has an answer here:
What is '1 in Collection type Name
(1 answer)
Closed 2 years ago.
I have this code in an if statements which evaluates to false and I don't have any clue why
&& (typeof(TResponse).Equals(typeof(MediatorResponse)))
used Equals as I intend to compare by reference
I tried to put them in watch and this is my clue
typeof(TResponse) seems to be a MediatorResponse'1 while typeof(MediatorResponse) seems to be a MediatorResponse are these 2 still of the same type?
Why does visual studio put 1 on the other?
C# allows you to have different types with the same name if one or more and generic, and if the generic types have a different number of generic arguments. For example, the following are all different types:
class Foo{}
class Foo<T>{}
class Foo<T1, T2>{}
The backtick character followed by a number is used on generic types to generate a unique name, and indicate the number of generic arguments the type has. So in the above example the names would be:
class Foo{} // Name is Foo
class Foo<T>{} // Name is Foo`1
class Foo<T1, T2>{} // Name is Foo`2
This question already has answers here:
Why enums require an explicit cast to int type?
(7 answers)
Closed 6 years ago.
I can define an enum with integer as value:
public enum FieldType
{
Bar = 0,
Foo = 1
}
Why C# doesn't allow creating a dictionary with integer as a key and using an enum value when creating:
public Dictionary<int, Type> _fieldMapping = new Dictionary<int, Type>
{
{FieldType.Bar, null}, # Error in VS
{FieldType.Foo, null}, # Error in VS
}
I know i can do a cast, but i would like to know why enum cannot be converted to int implicitly when it actually is int under the hood.
If you could do that, you'd lose half the point of using enums at all - the point is to keep them as separate types to avoid you making mistakes involving using types incorrectly.
It seems to me like your dictionary should actually have FieldType as the key - at which point you'd have a safer approach. With your current expectation, you're expecting to be able to use any (int-based) enum type as the key, including things like FileShare which are clearly inappropriate.
So basically, you should ask yourself whether fundamentally you've got a map from field type to type, or whether you've really got a numeric map. To put it another way, without looking at FieldType at all, does an entry in the map from the number 1 to some Type mean anything? What does the 1 value represent there?
This question already has answers here:
Extension method and dynamic object
(3 answers)
Closed 6 years ago.
I have a Extension Function named ParseLong for string.
public static long ParseLong(this string x, long Default = 0)
{
if (!string.IsNullOrEmpty(x))
long.TryParse(x, out Default);
return Default;
}
And works fine:
long x = "9".ParseLong();
However for dynamic objects like:
dynamic x = GetValues();
x.StartValue.ToString().ParseLong();
generates the error:
'string' does not contain a definition for 'ParseLong'
Correct, extension functions do not work for dynamic objects. That is because the dynamic object, when being told to execute ParseLong, has no clue what using directives were in your C# code, so cannot guess what you want to do.
Extension methods are 100% a compiler feature (only); dynamic is primarily a runtime feature (although the compiler has to help it in places).
You could just cast, though, if you know the type:
long x = ((string)x.StartValue).ParseLong();
(which swaps back from dynamic to regular C#, so extension methods work)
This question already has answers here:
C# Reflection: How to get class reference from string?
(6 answers)
Closed 8 years ago.
I am new to C# and writing some automation framework .I want to initialize a class dynamically based on condition .
i get the name of the class as a string based on conditions .
Ex : "Vehicle_"+ typeOfvehicle => Which will on run time may be Vehicle_2Wheeler or Vehicle_3Wheeler or Vehicle_4Wheeler .
I am using if , else statement for now . But if i can initialize the class with the type of Class i want to dynamically it would be better .
I think i need to use the Reflection API but not sure how to achieve this .
Please let me know if some one has an idea of this .
In C# Type.GetType("Truck") will return a Type that you can then instantiate
var type = Type.GetType("MyProject.Truck");
var instance = (Vehicle)Activator.CreateInstance(type);
Though if you don't know the specific type at compile time, leave off the cast, and just use object, dynamic, or a base class.
To pass args:
Activator.CreateInstance(type, arg1, arg2);