Structures and Enumerations Questions in C#

1. ) What is structure in C#?                                                                                               
A structure is an user defined data type which can contain different members as variable , Method ,properties etc. Structures are value type and stored on the stack memory.

2. ) What is the advantage of structure in C#?                                                               
Structures are stored on the stack,so there are some advantage of structures.
  • Structure are created more quickly than heap -allocated.
  • Structures are automatically deallocated ,when it go the out of the scope.
  • Structures are value type so that we can easily copy value type variables on the stack memory.
 3. ) How does define structure in C#?                                                                              
struct structure_name
data-member 1;
data-member 2;
data-member 3;
struct  Employee
public string Name;
public int  e-id;
public money salary;
Employee e1;

 4. ) How does  assign the values in  structure's member in C#?                              
e1. Name = 'Ram';
e1. e-id = 101;
e1. salary = 2000;

5. ) How does copy values from one structure to another structure in C#?        
 When we create object of any structure class,the data member of that structure automatic comes in memory.So if we want to copy values from one structure to another structure then we have to assigned the one  structure's object to another structure's object.Which is given below:
Employee e1 ;
Employee e2 ;
e1 = e2;                  (structure second  copied to first structure)

6. )Does Structure members private by default  in C#?                                              
Yes, Structure members are private by default in C#.We can not access private member  from the outside using dot operator.

7. )How can we use constructor and method  in C# program?                                  
using System;
namespace ConsoleApplication6
    class Program
        struct rectangle
        int x,y;
        public rectangle(int a, int b)     //Constructor
            x = a;
            y = b;
        public void Area()                // method
            Console.WriteLine("area of rectangle is: ");
        static void Main(string[] args)
            rectangle rect = new rectangle(20,30);


8. ) Can we use Nested Structures  in C# ?                                                                      

9. )What is difference between Structure and Class in C# ?                                        
More Details..

10. )Does C# support parameter less constructor by default  ?                                  
No, C# does not support parameter less constructor.

11. )What is Enumeration in C# ?                                                                                     
An Enumeration is an user-defined integer type,which is used for attaching names to Numbers .We use enum keyword for Enumeration in C#.

12. )What is the Syntax Enumeration in C# ?                                                                
enum {value 1,value 2,value 3, ........value n}
enum Days {mon,tue,wed,thu,fri,sat,sun}

13. )What is the value of first enum member by default in C# ?                              

14. )How does assign values in enum member manually in C# ?                              
enum color
white =1;

15. )What is the type of an enum by default in C# ?                                                    

16. )How can we declare explicitly a base type for each enum  in C# ?                     
The valid base types are:-
byte, sbyte, short, ushort, int ,uint , long , ulong
enum shape : long
rectangle ;
square ;
triangle =100;

Note:-The value assigned to the members must be within the range of values that can be represented by the base type in C#,Means we can assign value in enum members within range of base type.

17. )What is type conversion in Enumeration in C# ?                                                  
enum type can be converted to their base type.we can use  explicit conversion to convert back using Cast function. 
enum number
 value 0,
value 1,
value 2,
number n = (number) 1;
int x = int (int)n;

Note:- Literal values can be converted to an enum  type without any cast.

18. )Can i use Implicit and Explicit conversion in Enumerator ?                           

19. )When do we prefer the use of structs over class?                                               
We do not prefer structure if it satisfied the following statement which are given below:
  •  For represent a single value.
  • size is smaller than 16 byte.
  • immutable
  • for boxing 
20. )What is the purpose of a constructor in a Structure ?                                        
Constructors are basically  used for initialization of  the data members in the structure .


Post a Comment

Powered by Blogger.