Results 1 to 4 of 4

Thread: Variable method arguments in Csharp

  1. #1
    Join Date
    Sep 2009
    Posts
    143

    Variable method arguments in Csharp

    I have just started studying Csharp as it is in my syllabus. I have the experience of using Java in my earlier term. And this is helping me to study Csharp. My doubt is, are Variable method arguments supported in Csharp? I am not having any knowledge about this. I tried but, I am not getting a satisfactory answer. Can anyone please provide a precise explanation on this ?

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    Re: Variable method arguments in Csharp

    The answer to your question is that with the use of keyword params it is possible to have a Variable method arguments in Csharp. On a method parameter which is an array we can apply the keyword params. By seperating the element list of an array by comma(,) we can supply them on method invocation.
    eg:

    Code:
    void Disp(params object[] info)
    {
       foreach (object I in info)
       Console.WriteLine(I);
    }
    PrintOut("Hi", new DateTime(14, 11, 2008), 8);

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Variable method arguments in Csharp

    I had just completed Csharp basics. I also had hard time learning Csharp. I didnot had any knowledge of Java although I have studied HTML(Hyper Text Markup Language) which was of no use here. One of the features of Csharp is that it enables you to have variable method arguments in Csharp. You can achieve this by using the params keyword.

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    Re: Variable method arguments in Csharp

    Code:
    using System;
     
    class varpar
    {
       public static void out(params int[] anum)
       {
          foreach(int num in anum)
          {
             Console.WriteLine(num); 
          }
       }
     
       public static void Main (string[] args)
       {
          out(5, 21, 8);  
       }
    }
    From the above C# code it is very clear that C# supports variable method arguments. Thus a method in C# can have a variable argument list by making use of the params keyword. This is also a common feature between C# and Java.

Similar Threads

  1. How to input variable in string.find method in C++
    By Caelaan in forum Software Development
    Replies: 5
    Last Post: 29-01-2010, 10:50 PM
  2. String in the method arguments
    By DANIEL 602 in forum Software Development
    Replies: 3
    Last Post: 29-12-2009, 02:21 PM
  3. Function with a variable number of arguments
    By Chrisch in forum Software Development
    Replies: 3
    Last Post: 20-11-2009, 02:03 PM
  4. Syntax Error for a variable named checked in Csharp
    By KANAN14 in forum Software Development
    Replies: 3
    Last Post: 17-11-2009, 01:58 AM
  5. How to pass arguments to the main method
    By Wannabe in forum Software Development
    Replies: 3
    Last Post: 12-09-2009, 11:51 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,727,400,919.58562 seconds with 17 queries