Difference between classes and structs - Video
What do you mean by saying a "class is a reference type"?
A class is a reference type means when an object of the class is created, the variable to which the object is assigned holds only a reference to that memory. When the object reference is assigned to a new variable, the new variable refers to the original object. Changes made through one variable are reflected in the other variable because they both refer to the same data.
What do you mean by saying a "struct is a value type"?
A struct is a value type mean when a struct is created, the variable to which the struct is assigned holds the struct's actual data. When the struct is assigned to a new variable, it is copied. The new variable and the original variable therefore contain two separate copies of the same data. Changes made to one copy do not affect the other copy.
When do you generally use a class over a struct?
A class is used to model more complex behavior, or data that is intended to be modified after a class object is created. A struct is best suited for small data structures that contain primarily data that is not intended to be modified after the struct is created.
List the 5 different access modifiers in C#?
1. public
2. protected
3. internal
4. protected internal
5. private
If you donot specify an access modifier for a method, what is the default access modifier?
private
Classes and structs support inheritance. Is this statement true or false?
False, Only classes support inheritance. structs donot support inheritance.
If a class derives from another class, will the derived class automatically contain all the public, protected, and internal members of the base class?
Yes, the derived class will automatically contain all the public, protected, and internal members of the base class except its constructors and destructors.
Can you create an instance for an abstract class?
No, you cannot create an instance for an abstract class.
How do you prevent a class from being inherited by another class?
Use the sealed keyword to prevent a class from being inherited by another class.
Classes and structs can be declared as static, Is this statement true or false?
False, only classes can be declared as static and not structs.
Can you create an instance of a static class?
No, you cannot create an instance of a static class.
Can a static class contain non static members?
No, a static class can contain only static members.
If a Class derives from another class will the derived class automatically contain all public protected internal members of the base class?
ReplyDeleteIf the derived class is in a different assembly, internal members will not be accessible.(Protected Internal Members will be accessible across the assembly)
yes a derived class can have both public and protected members of base class.....but
ReplyDeletein the case the base class is in another assembly protected members can not be inherited
I want to send email using smtp server in asp.net c#. could you please help me.
ReplyDeleteThe default access modifier for a class is internal, because when we declare private to a class member it will throw an error saying a class can not be private, protected or protected internal.
ReplyDeleteA class can contain following 10 members
ReplyDelete------------------------------------------------
1.DataFields/classfields
2.Properties
3.Indexers
4.Constants
5.Nested Types
6.Constructors
7.Destructors
8.Events
9.Functions/Methods
10.Operators.