Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5974627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:58:09+00:00 2026-05-22T20:58:09+00:00

I am writing a class library in C# for working with matrices, and am

  • 0

I am writing a class library in C# for working with matrices, and am currently working on a subclass of Matrix called ComplexMatrix. The Matrix base class works with values of the Int32 data type (a more advanced version uses Double), and the ComplexMatrix of the System.Numerics.Complex structure (.NET 4).

For the base class, I overrode ToString() as:

| 1 2 |
| 3 4 |    printed as    {{1,2}{3,4}}

The System.Numerics.Complex structure overrides ToString() in the form:

a+bi    printed as    (a,b)    where a is real and b is imaginary

When overriding ToString in ComplexMatrix, I simply used the method:

public override string ToString()
{
    return base.ToString();
}

Unfortunately, for a complex matrix, the following occurred:

| 1+1i 1+2i |
| 2+1i 2+2i |   printed as   {{0,0}{0,0}} rather than {{(1,1),(1,2)}{(2,1)(2,2)}}

The original ToString() code I wrote for the Matrix class is:

public override string ToString()
{
    StringBuilder matrixString = new StringBuilder();
    string comma = "";

    matrixString.Append("{");
    for (int i = 0; i < this.Rows; i++)
    {
        matrixString.Append("{");
        for (int j = 0; j < this.Columns; j++)
        {
            if (j == 0) comma = "";
            else comma = ",";

            matrixString.Append(comma + this.Elements[i, j].ToString());
        }
        matrixString.Append("}");
    }
    matrixString.Append("}");
    return matrixString.ToString();
}

In the above code:

  • this.Elements Property: in the Matrix class, this is a 2-dimensional array of Int32 type (Double in a newer, more advanced version); it is of System.Numerics.Complex type in ComplexMatrix
  • this.Rows, this.Columns Properties: the number of rows and columns respectively of the matrix

Several questions I have are:

  • When ToString is called on a ComplexMatrix instance, and calls the base ToString() method, is an attempted type conversion taking place from Complex to Int32?
  • As the ComplexMatrix Elements property (Complex[,] type) is hiding the base class Elements property (Int32[,] type), the new keyword is required?
  • Is the “this” kwyword being seen as a Matrix type rather than ComplexMatrix?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T20:58:09+00:00Added an answer on May 22, 2026 at 8:58 pm

    I think your problem is the following:
    The Elements property is not marked virtual in the Matrix class. The Elements property in the ComplexMatrix class hides the Elements property of the Matrix class. Therefore polymorphism isn’t working and the ToString method in Matrix accesses Matrix.Elements and not ComplexMatrix.Elements. But because Elements is a property and you want to change the type of the property, you can’t use virtual anyway.

    To fix the problem, you should do something like this:

    • Create a generic base class that wants the type of the matrix values as generic parameter
    • Create a SimpleMatrix class that inherits from this base class and passes int as generic parameter.
    • Create a ComplexMatrix class that inherits from this base class and passes Complex as generic parameter.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently writing a class that implements the SeekableIterator interface and have run into
I'm currently writing a class to handle all database-activity in my application, and so
I'm currently writing a C#-class in my ASP.NET (3.5) application to handle all database-queries.
I'm currently working on a project which will support multiple file writing specifications (imagine
I've been working on writing a library in my spare time to familiarize myself
I am writing the necessary business logic in C# methods by using class library
I am writing a class library where i need to extend System.DateTime to have
I am writing a class library to perform operations on a site outside my
I am writing a class library that contains classes with purely internal members. I
I'm writing a class library (say: lib ) that parses and compiles Razor templates.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.