| |

VerySource

 Forgot password?
 Register
Search
View: 2247|Reply: 9

C # class and structural questions

[Copy link]

2

Threads

5

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2021-4-25 19:00:02
| Show all posts |Read mode
In the class, the field can be assigned;

Class C
{
    INT i = 123; // can
}

In the structure, it is not possible.

Struct S
{
    INT i = 123; //
}

Why can't you assign a initial value in the structure, what is the benefit? Does this flexibility are not lowered?
Reply

Use magic Report

0

Threads

110

Posts

63.00

Credits

Newbie

Rank: 1

Credits
63.00

 China

Post time: 2021-4-25 19:15:01
| Show all posts
Yes, but put it in the constructor..

For example:

Public struct coords
{
    Public Int x, y;

    Public Coords (int P1, INT P2)
    {
        X = p1;
        Y = p2;
    }
}
Reply

Use magic Report

2

Threads

5

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2021-4-25 19:30:01
| Show all posts
The structure can do, the class can be done, is the structure not?
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2021-4-25 19:45:01
| Show all posts
Their side focus, each has its own strengths.
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2021-4-25 20:00:02
| Show all posts
The biggest difference is that one is a value type, one is a reference type. One assigns memory in the stack, one of the allocated memory in the stack. The introduction structure in the C # is mainly to consider using some of the previous APIs, and there is a packing problem in the structure, and the class does not exist.
In a word, although they look very like, it is indeed different.
Reply

Use magic Report

0

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2021-4-25 20:15:02
| Show all posts
Their difference:


Problem structure
Is the value type or a reference type? Value type reference type

What examples are placed on the stack or on the stack? The instance of the structure is the value, and the example existing in the stack is called the right
                                                                      Icon, existing on the pile

Can you declare a default constructor? Cannot be

If you declare your constructor, it will not
Does the compiler still generate a default constructor?

If you don't initialize in your own constructor, you will not
Will the compiler will initialize you?

Can I initialize it while declaring an instance field? Cannot be
Reply

Use magic Report

0

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2021-4-25 20:30:01
| Show all posts
(Problem) (structure) (class)
Is the value type or a reference type? Value type reference type

What examples are placed on the stack or on the stack? The instance of the structure is value, and the example of the class is called
                                     Existing in the stack, existing on the pile
                 

Can you declare a default constructor? Cannot be

If you declare your constructor, it will not
Does the compiler still generate a default constructor?

If you don't initialize in your own constructor, you will not
Will the compiler will initialize you?

Can I initialize it while declaring an instance field? Cannot be
Reply

Use magic Report

0

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2021-4-25 20:45:01
| Show all posts
Sorry, if you can support the form, it will be better!
Reply

Use magic Report

2

Threads

5

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 Sweden

 Author| Post time: 2021-4-25 21:00:01
| Show all posts
Thank you, I will record it, I will experience ^ - ^
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 United States

Post time: 2021-9-17 17:34:55
| Show all posts
5楼正解
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list