如何在c#中定义类外的构造函数

时间:2014-03-10 15:14:11

标签: c# c++

我是c#的新手,刚从c ++切换到c#。 我在c ++中做了类似的事情:

Class A
{
 public : A(char *argv);//declaration of constructor
}

然后在主要我这样做:

int main(int argc, char **argv)
{
 A Obj(argv[1]);
} 

然后定义构造函数我喜欢这样:

A::A(char * argv) 
{
 //Here i use this command line argument argv which contains a file.
}

我尝试在c#中编写等效代码,如下所示:

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace shekhar_final
 {
    class Huffman 
    {
    public  int data_size,length,i,is_there, total_nodes;
    string code;
    Huffman(char  *args);
    }

        public   Huffman(char  *args) //called from MyClass  Line:16
        {
            using (var stream = new BinaryReader(System.IO.File.OpenRead(args[0])))  //Line : 18
            {
                while (stream.BaseStream.Position < stream.BaseStream.Length)
                {
                    byte processingValue = stream.ReadByte();
                }
            }
        }

    public class MyClass 
    {
        public static void Main(string[] args)
        {       
         Huffman ObjSym =new Huffman(args);//object creation
        }
    }
}// Line:34

我得到的几个错误是:// 我已经指出对应于我的代码中的错误的行

shekhar_c#.cs(16,25): error CS1525: Unexpected symbol `Huffman', expecting `class', `delegate', `enum', `interface', `partial', or `struct'
shekhar_c#.cs(18,33): error CS1530: Keyword `new' is not allowed on namespace elements
shekhar_c#.cs(18,36): error CS1525: Unexpected symbol `BinaryReader', expecting `class', `delegate', `enum', `interface', `partial', or `struct'
shekhar_c#.cs(18,79): warning CS0658: `value' is invalid attribute target. All attributes in this attribute section will be ignored
shekhar_c#.cs(34,1): error CS8025: Parsing error
Compilation failed: 4 error(s), 1 warnings

请你帮我编写c#等效的c ++(删除这些错误)。我也欢迎额外的指导,因为我是c#的初学者。

6 个答案:

答案 0 :(得分:3)

与C ++不同,你可以选择在头文件中组合声明和成员函数的定义,或者将声明放在头文件和cpp文件中的实现中,在C#中没有这样的选择:如果是函数有一个主体(即它不是抽象的),主体需要成为声明的一部分:

class Huffman 
{
    public  int data_size,length,i,is_there, total_nodes;
    string code;
    Huffman(string args) {
        using (var stream = new BinaryReader(System.IO.File.OpenRead(args)))
        {
            while (stream.BaseStream.Position < stream.BaseStream.Length)
            {
                byte processingValue = stream.ReadByte();
            }
        }
    }
}

答案 1 :(得分:2)

您没有在C#中提前定义方法 - 它们是在类本身内定义的。试试这个:

class Huffman 
{
public  int data_size,length,i,is_there, total_nodes;
string code;

    public Huffman(char *args) //called from MyClass  Line:16
    {
        using (var stream = new BinaryReader(System.IO.File.OpenRead(args[0])))  //Line : 18
        {
            while (stream.BaseStream.Position < stream.BaseStream.Length)
            {
                byte processingValue = stream.ReadByte();
            }
        }
    }
}

public class MyClass 
{
    public static void Main(string[] args)
    {       
         Huffman ObjSym =new Huffman(args); //Here is the error
    }
}

答案 2 :(得分:2)

在C#中,声明和实现结合在一起:

namespace shekhar_final
{
    class Huffman 
    {
        public int DataSize {get; set;}
        public int Length {get; set;}
        public int I {get;set;}
        public int IsThere {get;set;}
        public int TotalNodes {get;set;}
        private string code;

        public Huffman(string[] args) //called from MyClass  Line:16
        {
            using (var stream = new BinaryReader(System.IO.File.OpenRead(args[0])))  //Line : 18
            {
                while (stream.BaseStream.Position < stream.BaseStream.Length)
                {
                    byte processingValue = stream.ReadByte();
                }
            }
        }
    }

    public class MyClass 
    {
        public static void Main(string[] args)
        {       
           Huffman objSym = new Huffman(args);//object creation
        }
    }
}// Line:34

答案 3 :(得分:2)

C#和C ++之间的主要哲学是不同的。在C ++中,您有一个头文件和一个实现文件。在C#中,everthing需要在一个类中。因此,您将构造函数声明为类并将实现放在其中。

class funny {
    public funny() {
     ...  add your constructor stuff here
    }
    ... other stuff ...
 }

答案 4 :(得分:1)

在c#中,您不会将声明和定义分开。由于所有类型在程序集中一起存在,因此c#中没有声明这样的概念。如果您希望在c3中为类使用多个文件,则可以使用partial classes的概念。

答案 5 :(得分:1)

C#要求在类中定义构造函数:

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace shekhar_final
{
    public class Huffman{

    public  int data_size,length,i,is_there, total_nodes;
    string code;

     public   Huffman(string[]  args) //called from MyClass  Line:16
     {
         using (var stream = new BinaryReader(System.IO.File.OpenRead(args[0])))  //Line : 18
         {
             while (stream.BaseStream.Position < stream.BaseStream.Length)
             {
                 byte processingValue = stream.ReadByte();
             }
         }
      }
    }

   public class MyClass 
   {
       public static void Main(string[] args)
       {       
         Huffman ObjSym =new Huffman(args);//object creation
       }
   }
}// Line:34