Skip to content

Commit 026b76e

Browse files
Missing Code Included for Examples
1 parent 7acb1a9 commit 026b76e

File tree

1 file changed

+60
-10
lines changed

1 file changed

+60
-10
lines changed

content/blog/interview/questionsetsdumps/DoYouKnowThese.md

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,23 @@ This code runs on the .NET framework.
178178
- *Example:*
179179

180180
```csharp
181-
public sealed class FinalClass { }
181+
public sealed class SealedClass
182+
{
183+
public void Hello()
184+
{
185+
Console.WriteLine("Hello");
186+
}
187+
}
188+
```
189+
```csharp
190+
public class CanInHeritSealedClass :SealedClass
191+
{
192+
193+
}
182194
```
195+
- On Inheriting selaed class gives
196+
> CanlnHeritSea1edC1ass ' : cannot derive from sealed type 'SealedC1ass'
197+
183198

184199
4. **Static Classes:** Classes that cannot be instantiated and only contain static members.
185200
- *Example:*
@@ -207,10 +222,26 @@ Yes, you can prevent object creation by making the class `abstract` or by provid
207222
- **Private Constructor:**
208223

209224
```csharp
210-
public class Singleton
211-
{
212-
private Singleton() { }
213-
}
225+
public class Singleton
226+
{
227+
private static readonly Singleton _instance = new Singleton();
228+
229+
// Private constructor prevents instantiation from other classes
230+
private Singleton() { }
231+
232+
public static Singleton Instance
233+
{
234+
get
235+
{
236+
return _instance;
237+
}
238+
}
239+
240+
public void DoSomething()
241+
{
242+
Console.WriteLine("Singleton instance is working.");
243+
}
244+
}
214245
```
215246

216247
### Q8. What is Property?
@@ -234,7 +265,10 @@ A property is a member of a class that provides a mechanism to read, write, or c
234265
### Q9. What is the difference between Property and Function?
235266

236267
- **Property:** Used to access or modify the value of a field. It appears like a field but is actually a method behind the scenes.
237-
- *Example:* `public string Name { get; set; }`
268+
- *Example:*
269+
```csharp
270+
public string Name { get; set; }
271+
```
238272

239273
- **Function (Method):** Performs an operation and may return a value or perform a task. Methods can take parameters and may include logic that properties generally do not.
240274
- *Example:*
@@ -299,16 +333,32 @@ In this example, `Dog` inherits the `Eat` method from `Animal` and has its own m
299333
### Q12. What are the different types of Inheritance?
300334

301335
1. **Single Inheritance:** A class inherits from a single base class.
302-
- *Example:* `class Derived : Base { }`
336+
- *Example:*
337+
```csharp
338+
public class Derived : Base { }
339+
```
303340

304341
2. **Multiple Inheritance:** A class inherits from multiple base classes. (Not supported in C# directly, but can be achieved using interfaces.)
305-
- *Example:* `class Derived : IInterface1, IInterface2 { }`
342+
- *Example:*
343+
```csharp
344+
public class Derived : IInterface1, IInterface2 { }
345+
```
306346

307347
3. **Multilevel Inheritance:** A class inherits from a derived class which itself inherits from another class.
308-
- *Example:* `class Grandparent { } class Parent : Grandparent { } class Child : Parent { }`
348+
- *Example:*
349+
```csharp
350+
public class Grandparent { }
351+
public class Parent : Grandparent { }
352+
public class Child : Parent { }
353+
```
309354

310355
4. **Hierarchical Inheritance:** Multiple classes inherit from a single base class.
311-
- *Example:* `class Base { } class Derived1 : Base { } class Derived2 : Base { }`
356+
- *Example:*
357+
```csharp
358+
public class Base { }
359+
public class Derived1 : Base { }
360+
public class Derived2 : Base { }
361+
```
312362

313363
5. **Hybrid Inheritance:** A combination of more than one type of inheritance.
314364
- *Example:* Combining hierarchical and multilevel inheritance.

0 commit comments

Comments
 (0)