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

How to define constructor outside the class in c#

本文关键字:构造函数 定义      更新时间:2023-10-16

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

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

然后在main中我这样做:

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#的初学者。

与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();
            }
        }
    }
}

在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

在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
    }
}

c#和c++的主要原理是不同的。在c++中,你有一个头文件和一个实现文件。在c#中,一切都需要在类中。因此,您可以声明类的构造函数,并将实现放在其中。

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

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

在c#中你不分离声明和定义。c#中没有声明这样的概念,因为所有类型都存在于一个程序集中。如果您希望在c3中为类使用多个文件,您可以使用部分类的概念。