*
Previous WPF-Question-Answers-31-40 C# Question Answers-11 - 20 Next

C# (C-Sharp) Question Answers - 1 - 10

1. Are private class-level variables inherited?

Yes, but they are not accessible. Although they are not visible or accessible via the class interface, they are inherited.

2. Below code is valid?

Font font = new Font("Arial", 10.0f)
using (font)
{
   // use font here
}

Yes, we can use the object declared and instantiated before the using statement.

3. C# provides a default constructor for me. I write a constructor that takes a string as a parameter, but want to keep the no parameter one. How many constructors should I write?

Two. Once you write at least one constructor, C# cancels the freebie constructor, and now you have to write one yourself, even if there’s no implementation in it.

4. Can multiple catch blocks be executed for a single try statement?

No. Once the proper catch block is processed, control is transferred to the finally block (if there are any).

5. Can we use multiple Catch clause in try statement?

Yes, we can use multiple Catch clause in try statement.

6. Can you allow a class to be inherited, but prevent the method from being over-ridden?

Yes. Just leave the class public and make the method sealed.

7. Can you declare the override method static while the original method is non-static?

No, you can’t — the signature of the virtual method must remain the same; only the keyword virtual is changed to the keyword override.

8. Can you inherit multiple interfaces?

Yes. .NET does support multiple interfaces.

9. Can you make immutable class in C#

Examples:

  • // This class is immutable. After an object is created, 
    // it cannot be modified from outside the class. It uses a 
    // constructor to initialize its properties. 
    class Contact
    {
        // Read-only properties. 
        public string Name { get; }
        public string Address { get; private set; }
    
        // Public constructor. 
        public Contact(string contactName, string contactAddress)
        {
            Name = contactName;
            Address = contactAddress;               
        }
    }
  • // This class is immutable. After an object is created, 
    // it cannot be modified from outside the class. It uses a 
    // static method and private constructor to initialize its properties.    
    public class Contact2
    {
        // Read-only properties. 
        public string Name { get; private set; }
        public string Address { get; }
    
        // Private constructor. 
        private Contact2(string contactName, string contactAddress)
        {
            Name = contactName;
            Address = contactAddress;               
        }
    
        // Public factory method. 
        public static Contact2 CreateContact(string name, string address)
        {
            return new Contact2(name, address);
        }
    }

10. Can you prevent your class from being inherited by another class?

Yes. The keyword sealed will prevent the class from being inherited.

Back to Index
Previous WPF-Question-Answers-31-40 C# Question Answers-11 - 20 Next
*
*