out-parameters

C# - how to pass 'out' parameter into lambda expression

喜夏-厌秋 提交于 2019-12-05 00:08:57
I have a method with the following signature: private PropertyInfo getPropertyForDBField(string dbField, out string prettyName) In it, I find the associated value prettyName based on the given dbField . I then want to find all properties, if any, that have the name prettyName , so I'm trying to do the following: IEnumerable<PropertyInfo> matchingProperties = getLocalProperties().Where(prop => prop.Name.Equals(prettyName) ); However, this gives the following error: Cannot use ref or out parameter 'prettyName' inside an anonymous method, lambda expression, or query expression By the point in the

Why is an out parameter not allowed within an anonymous method?

蓝咒 提交于 2019-12-04 16:49:22
问题 This is not a dupe of Calling a method with ref or out parameters from an anonymous method I am wondering why out parameters are not allowed within anonymous methods. Not allowing ref parameters makes a bit more sense to me, but the out parameters, not as much. what are your thoughts on this 回答1: In some ways this is a dupe. Out parameters are ref parameters. There is simply an extra attribute on the value that is used by the C# language. The reason for disallowing them is the exact same as

When is the value of a C# 'out' or 'ref' parameter actually returned to the caller?

前提是你 提交于 2019-12-04 16:44:20
问题 When I make an assignment to an out or ref parameter, is the value immediately assigned to the reference provided by the caller, or are the out and ref parameter values assigned to the references when the method returns? If the method throws an exception, are the values returned? For example: int callerOutValue = 1; int callerRefValue = 1; MyMethod(123456, out callerOutValue, ref callerRefValue); bool MyMethod(int inValue, out int outValue, ref int refValue) { outValue = 2; refValue = 2;

Why don't anonymous delegates/lambdas infer types on out/ref parameters?

こ雲淡風輕ζ 提交于 2019-12-04 10:28:15
问题 Several C# questions on StackOverflow ask how to make anonymous delegates/lambdas with out or ref parameters. See, for example: Calling a method with ref or out parameters from an anonymous method Write a lambda or anonymous function that accepts an out parameter To do so, you just need to specify the type of the parameter, as in: public void delegate D(out T p); // ... D a = (out T t) => { ... }; // Lambda syntax. D b = delegate(out T t) { ... }; // Anonymous delegate syntax. What I'm

Code analysis comes back with suggestion about not using “out” parameters

戏子无情 提交于 2019-12-04 07:07:25
I ran the VS 2008 code analysis tool against an object I created and received the following suggestion ... Warning 147 CA1021 : Microsoft.Design : Consider a design that does not require that 'returnValue' be an out parameter. I find "out" parameters rather useful and didn't realize that they were considered as a frowned upon design practice. I wanted to know if someone could shed some light on the reason that I received this Warning? If it is bad practice? why? and what would be good practice? I appreciate any advice. Every Code Analysis warning has associated documentation that you can

C#: How to use generic method with “out” variable

ぃ、小莉子 提交于 2019-12-04 04:58:20
I want to create a simple generic function void Assign<T>(out T result) { Type type = typeof(T); if (type.Name == "String") { // result = "hello"; } else if (type.Name == "Int32") { // result = 100; } else result = default(T); } Usage: int value; string text; Assign(value); // <<< should set value to 100 Assign(text); // <<< should set text to "hello" My question is how do you program the code to set these values ie. the missing codes in comment section. Thanks for any help. It looks like in this case maybe you're doing it to try to avoid boxing? Difficult to say without more information, but

passing out parameter

你离开我真会死。 提交于 2019-12-03 23:29:53
问题 I wrote a method with an out parameter: -(NSString *)messageDecryption:(NSString *)receivedMessage outParam:(out)messageCondent { messageCondent = [receivedMessage substringFromIndex:2]; return [receivedMessage substringToIndex:1]; } Then I passed the param like this: NSString *messageCondent; NSString *mode = [myclassobject messageDecryption:message outParam:messageCondent]; However, there is a problem. The out parameter value is not being set properly. Can any one help me to do this

Java MyBatis stored procedure call with OUT parameters

安稳与你 提交于 2019-12-03 15:14:29
First question: I am trying to return one OUT parameter and not a result set with annotations. First, is it even possible? If it is, how would one do this? MyBatis: 3.0.6 Database: SQL Server 2008 Here is an example of the syntax of my method call in the UserDAO: @Select(value= "{ CALL saveUser( " + "#{userId, mode=IN, jdbcType=INTEGER}," + "#{firstname, mode=IN, jdbcType=VARCHAR}," + "#{lastname, mode=IN, jdbcType=VARCHAR}," + "#{message, mode=OUT, jdbcType=VARCHAR}" + ")}") @Options(statementType=StatementType.CALLABLE) public String saveUser( @Param("userId") int userId, @Param("firstname")

Why is an out parameter not allowed within an anonymous method?

£可爱£侵袭症+ 提交于 2019-12-03 11:40:00
This is not a dupe of Calling a method with ref or out parameters from an anonymous method I am wondering why out parameters are not allowed within anonymous methods. Not allowing ref parameters makes a bit more sense to me, but the out parameters, not as much. what are your thoughts on this JaredPar In some ways this is a dupe. Out parameters are ref parameters. There is simply an extra attribute on the value that is used by the C# language. The reason for disallowing them is the exact same as ref parameters. The problem here originates with the effect of using a value declared outside the

When is the value of a C# 'out' or 'ref' parameter actually returned to the caller?

▼魔方 西西 提交于 2019-12-03 10:42:24
When I make an assignment to an out or ref parameter, is the value immediately assigned to the reference provided by the caller, or are the out and ref parameter values assigned to the references when the method returns? If the method throws an exception, are the values returned? For example: int callerOutValue = 1; int callerRefValue = 1; MyMethod(123456, out callerOutValue, ref callerRefValue); bool MyMethod(int inValue, out int outValue, ref int refValue) { outValue = 2; refValue = 2; throw new ArgumentException(); // Is callerOutValue 1 or 2? // Is callerRefValue 1 or 2? } Since ref and