0

I am not sure, if this is right place to ask this question. Moderator can move this question to appropriate place if required.

I have question in C#.NET

I have below class with overloaded methods,

public class A
{
 public void GetNumber(short n)
 {
 }

 public void GetNumber(int n)
 {
 }

 public void GetNumber(long n)
 {
 }
}

A a = new A();
a.GetNumber(100);

so it will call method with int parameter, My Question is Why it will call method with int Parameter.

also on similar lines,

var n = 100;

in this case data type of n will also be set to int.

Why not short or other numeric data type.

Is there any logic/reason behind this or its just Microsoft has set this default standard for number data type.

Ashish Sapkale
  • 540
  • 2
  • 13
  • Wait, what? I don't see any `add` method in your `A` class – Rafalon Aug 09 '18 at 07:54
  • Yes it's as simple as this: the compiler - by specification - interpretes every number literal (that fits in an `Int32`) as `Int32`. To make it use a different type there are symbols like `m` for `decimal`, `L` for `long` etc... or you need to cast explicitly. – René Vogt Aug 09 '18 at 07:55
  • @Rafalon Sorry updated the code – Ashish Sapkale Aug 09 '18 at 07:57

0 Answers0