class Program
{
public struct course
{
public string name;
public int elecode;
public int credit;
public static void getdetails()
{
Console.WriteLine("Enter your Name");
Ele.name = Console.ReadLine();
}
}
static void Main(string[] args)
{
course ele;
ele.getdetails();
}
}
Member functions inside the structure: Structures in C cannot have member functions inside a structure but Structures in C++ can have member functions along with data members.
No, you can't. Structs can only contain variables inside, storing function pointers inside the struct can give you the desired result. Save this answer.
Accessing data fields in structs Although you can only initialize in the aggregrate, you can later assign values to any data fields using the dot (.) notation. To access any data field, place the name of the struct variable, then a dot (.), and then the name of the data field.
Function pointers can be stored in variables, structs, unions, and arrays and passed to and from functions just like any other pointer type. They can also be called: a variable of type function pointer can be used in place of a function name.
getdetails
should not be staticEle.
inside getdetails
course ele
variableYour code:
class Program
{
public struct course
{
public string name;
public void getdetails()
{
Console.WriteLine("Enter your Name");
name = Console.ReadLine();
}
}
static void Main(string[] args)
{
course ele = new course();
ele.getdetails();
}
}
As was mentioned by @DavidHeffernan in comment about poor design, you have to know where to use class
instead of struct
to escape problems, when a value type gives you a COPY of the value
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With