'varags'에 해당되는 글 2건

  1. 2010.04.08 C# Variable Arguments (Varargs)
  2. 2010.04.08 Java Variable Arguments (Varargs)

A public or protected type contains a public or protected method that uses the VarArgs calling convention.

Rule Description
The VarArgs calling convention is used with certain method definitions that take a variable number of parameters. A method using the VarArgs calling convention is not Common Language Specification (CLS) compliant and might not be accessible across programming languages.

In C#, the VarArgs calling convention is used when a method's parameter list ends with the __arglist keyword. Visual Basic does not support the VarArgs calling convention, and Visual C++ allows its use only in unmanaged code that uses the ellipse ... notation.

How to Fix Violations
To fix a violation of this rule in C#, use the params (C# Reference) keyword instead of __arglist.

When to Exclude Warnings
Do not exclude a warning from this rule.

The following example shows two methods, one that violates the rule and one that satisfies the rule.

using System;

namespace UsageLibrary
    public class UseParams 
        // This method violates the rule.
        public void VariableArguments(__arglist) 
            ArgIterator argumentIterator = new ArgIterator(__arglist);
            for(int i = 0; i < argumentIterator.GetRemainingCount(); i++) 
                    __refvalue(argumentIterator.GetNextArg(), string));

        // This method satisfies the rule.
        public void VariableArguments(params string[] wordList)
            for(int i = 0; i < wordList.Length; i++) 

[출처] Use params for variable arguments

Posted by bboks.net

댓글을 달아 주세요

The varargs, or variable arguments, feature allows a developer to declare that a method can take a variable number of parameters for a given argument. The vararg must be the last argument in the formal argument list.

You might use varargs to allow adding one to many objects to a collection that you have defined by using a single add method call. Suppose you need a method that writes any number of records to the console. Using varargs, you can make any of the following method calls:

write("line 1");
write("line 2", "line 3");
write("line 4", "line 5", "line 6", "line 7", "line 8", "line 9", "line 10");

Before 1.5, you would need to define overloaded versions of the write method to accomplish this. This isn't an effective solution if you want to be able to support any number of records being passed in.

The write method introduces a couple new bits of syntax:

public void write(String... records) {
    for (String record: records)

First, the records argument is defined as type String.... This indicates to the compiler that calling code can pass a variable number of String parameters. For all other intents and purposes, however, String... equates to a String array (String[]).

The Enhanced For Loop
Second, the code uses the new enhanced for loop syntax that is being introduced in Java 1.5. You read the for loop in the example above as "for each String record in records." Or more explicitly, "for each object stored in the String array records, assign the object to a reference named record of type String."

The enhanced for loop, also known as foreach, is a nice feature that can also be used with any of the collection classes in Java 1.5. In addition, Java supplies hooks that allow you to make your own class iterable, so that it can be referenced in a foreach loop.

Behind the Scenes
There isn't any real magic going on with respect to varargs. Behind the scenes, Java is converting the new syntax into equivalent code that would work under Java 1.4 and older versions. The example above is equivalent to coding the following:

write(new String[] { "line 1" });
write(new String[] { "line 2", "line 3" });
write(new String[] { "line 4", "line 5", "line 6"});
// ... 
public void write(String[] records) {
    for (String record: records)

[출처] Looking at Varargs in J2SE 1.5

Posted by bboks.net
TAG Java, varags

댓글을 달아 주세요