Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method Parameter Without A Parameter Type In C#

Tags:

c#

c#-4.0

I try to use trial version of third party software written C#. While I try to use its API, I face up with a strange problem.

I am using visual studio 2015 and I found an interesting extended method signature in their API-SDK

  public static class CallExtendedInfoEx
  {
        public static void AddToSIPMessage(this CallExtendedInfo info,  msg);
  }

The second parameter type is not exist.Never seen before in C#. I try to give a generic Object parameter as second parameter, and C# Compiler gives a strange error

Severity Code Description Project File Line

Error CS1503 Argument 2: cannot convert from 'object' to '.'

It works with "null"...No compiler error or run time error [ ignored i think ]

How those guys can able to write such a code in C#? Is It Posssible? Any idea?

Note : I suspect from a code obscurification problem...But still i do not understand what is going here

  IL_0073:  call       void [CallExtendedInfoEx::AddToSIPMessage(class [VoIP.CallExtendedInfo,
                                                                                  class [VoIPSDK]''.'')
like image 453
Hippias Minor Avatar asked Sep 01 '15 12:09

Hippias Minor


People also ask

What is parameters without type?

In the above function definition variable ' b' is the parameter and the value passed to the variable. 'b' is the argument.  The precondition (requires) and postcondition (returns) of the function is given.  We have not mentioned any types: (data types). This is called parameter without type.

Can a function have no parameters in C?

If a function takes no parameters, the parameters may be left empty. The compiler will not perform any type checking on function calls in this case. A better approach is to include the keyword "void" within the parentheses, to explicitly state that the function takes no parameters.

Can a function be without parameters?

The lesson brief states that “Functions can have zero, one or more parameters”.

How do you call a function with no parameters?

In C, void no_args() declares a function that takes an unspecified (but not variable) number of parameters (and returns nothing). So all your calls are valid (according to the prototype) in C. In C, use void no_args(void) to declare a function that truly takes no parameters (and returns nothing).


2 Answers

Well, looking at the ILDASM output, it's rather obvious that the argument has a perfectly valid type. Sure, it's a type that you can't actually name in C#, but it's perfectly valid in CLR at large.

The fact that you can pass null shouldn't be surprising - the argument is a reference type (or convertible from null), so null is a perfectly valid value. On the other hand, expecting passing an object instance is entirely wrong - you can only pass more derived types as argument, not less. It's fine to pass string (more specific) instead of object (less specific), but not the other way around.

Most likely, this is either a method you shouldn't touch (i.e. not part of the public contract of the API/SDK), or you're expected to get instances of the argument from some other method - never using the explicit type anywhere.

like image 68
Luaan Avatar answered Oct 29 '22 08:10

Luaan


This is not valid C# code. Some decompiler probably gave you that code, or the documentation where you copied this out of is faulty.

like image 29
usr Avatar answered Oct 29 '22 10:10

usr