1
    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace ConsoleApp1
    {
    // I am using structure to define values type set 
        public struct Employee
        {
            public int EmpId;
            public string FirstName;
            public string LastName;

    // Here i am checking whether parameter less constructor will work 
            static Employee()
            {
                Console.WriteLine("First Object Created");
            }

// Here i am using one more constructor for initializing it with some values later on
            public void Employee(int number, string firsname, string lasname)
            {
                id = number;
                FirstName = firsname;
                LastName = lasname;
            }    
        }    

        // Here i am using the parameterize constructor to assign values mentioned before in the //structure

        class Program {

            Employee emp1 = new Employee(23,"shiva","shankar");            
        }      
    }

// Error i am getting is Employee structure doesnt contain constructor that takes 3 args // The name id doesnt exist in current context //Employee : member names cannot be same as their enclosing type

2
  • First off you're probably better off using a class, but if you do use a struct do not allow it to be mutable as that will allow you to modify a copy which does not modify the original. (better to not allow mutation and force uses of the struct to assign a new value) stackoverflow.com/questions/441309/why-are-mutable-structs-evil Commented Jan 26, 2020 at 16:26
  • 1
    FYI a general rule of thumb is that if the type contains reference types (like string) then it probably should be a reference type. Commented Jan 26, 2020 at 16:34

3 Answers 3

2

the constructor doesn't have a return value.

public Employee(int number, string firsname, string lasname)
Sign up to request clarification or add additional context in comments.

Comments

2

There is no id field in your Employee struct, only EmpId. Also, constructor in C# can't have a return type:

A constructor is a method whose name is the same as the name of its type. Its method signature includes only the method name and its parameter list; it does not include a return type.

You should rewrite your code as following

// Here i am using one more constructor for initializing it with some values later on
public Employee(int number, string firstName, string lastName)
{
    EmpId = number;
    FirstName = firstName;
    LastName = lastName;
}

Also it makes sense to declare your fields as private, or use readonly properties instead, with getters only, initialized in constructor

public int EmpId { get; }
public string FirstName { get; }
public string LastName { get; }

Otherwise your struct is mutable. Also it contains a reference type values, like string. It's better to introduce an Employee class rather than struct

Comments

0

Well, it seems it's need to define a new static member, then call that new one, so the static constructor will be triggered soon.

Let's to check this:

public static int id = 0;

static Employee()
{
    Console.WriteLine("First Object Created");
}

First call:

Emploee.id = 1;
Employee emp1 = new Employee(23,"shiva","shankar");

Console.WriteLine("Id: {0}", Employee.id);
Console.WriteLine("emp1.EmpId: {0}", emp1.EmpId);

Output:

First Object Created
Id: 1;
emp1.EmpId: 23

Second call:

Employee.id = 2;

// copy emp1 values to var emp2
var emp2 = emp1; // both emp1 and emp2 have same values

// set emp2.EmpId to new value.
emp2.EmpId = 24;

Console.WriteLine("Id: {0}", Employee.id);
Console.WriteLine("emp1.EmpId: {0}", emp1.EmpId);
Console.WriteLine("emp2.EmpId: {0}", emp2.EmpId);

Output:

Id: 2;
emp1.EmpId: 23
emp2.EmpId: 24

Hope this could helps. Cheers!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.