DEV Community

Rasheed K Mozaffar
Rasheed K Mozaffar

Posted on • Edited on

2

C# Q&A : Part 1

Welcome to this new series of C# Q&A.
This's a 2 part series and this's part 1 , from now onwards , I'll be covering many questions which are going to be distributed on a 2 posts long series , the series will cover 20 questions , some will be accompanied with coding examples , however , everything is going to be beginner friendly and I'll try my best to further simplify any perplexing concepts if we stumbled upon any.

So , without further ado , let's kick off part 1

1: What's a class ?

A class is a defining structure or a template to create an object , it contains properties , data fields , indexers and methods.

We can create many instances of a class.

2: What's a namespace ,And is it compulsory ?

A namespace is basically a way of wrapping/organizing classes of the same group or relative functionality under the same name , which is up to you to specify , by importing a namespace to your code file , you can access all the grouped functionality that resides within that namespace , I like to think of a namespace as a tool box , when you bring that box and open it , you can start employing the tools inside it and put them to work.

One namespace we all use is the System namespace , by doing the following :

using System;
Enter fullscreen mode Exit fullscreen mode

By adding this line to our code , we can start using things like the Console Static Class and the like.

Are namespaces compulsory ? No
But it's a best practice so you end up with more maintainable code as an outcome.

3: What're Value Types & Reference Types ?

Value Type: Contains the value directly , an example could be this:

int i = 10;
Enter fullscreen mode Exit fullscreen mode

Reference Type: Contains a reference to a memory address in which the value is stored , an example would be a string like the following:

string message = "Hello , DevCommunity !";
Enter fullscreen mode Exit fullscreen mode

The message variable doesn't directly contain the value given to it , rather it contains a reference for the memory address where the message is stored.

4: What're the comment variations in C# ?

1: Single-line comments //A single line comment
2: Mutli-line comments /*A multi line comment */\
3: XML comments ///<summary>

5: Is C# code managed ?

Yes , C# code is managed cause the Common Language Runtime or CLR for short , compiles the code to Microsoft Intermediate Language or MSIL.

Managed Code: Managed code is essentially code whose execution is managed by a runtime.

6: What's an interface ?

An interface , like a class , can contain properties , indexers , and methods , although , unlike a class , interfaces only include the declaration of the members and not the concrete implementation , an interface might also be referred to as a contract , it merely contains declarations , and the implementation of the members is then left to the classes which implement that interface.

7: How to implement an interface ?

Well , implementing an interface is identical to inheriting a class , an imaginary example snippet could be used for demonstration purposes goes like this:

public interace IService
{
    public void SayHi();
}

public class Greeter : IService
{
    public void SayHi()
    {
        Console.WriteLine("Hi There !");
    }
}
Enter fullscreen mode Exit fullscreen mode

Quick Note: It's a convention when naming interfaces to precede the interface name with an I , it doesn't have anything to do with errors or anything else , but it's just a convention to follow , so always name your interfaces like that.

8: What's the virtual keyword used for ?

When a member is marked as virtual , it must be implemented in the class where it's defined , BUT , it can then be overridden by the classes inheriting that class in which the virtual member lives.

9: What's method overloading ?

It's possible to have methods that share the same name in the same class , although , they shouldn't match in their signatures , but what's a method signature ?
A method signature is the method's name and set of parameters it accepts.
An example of method overloading is the WriteLine method that belongs to the Console static class , this method has 18 overloads , they all differ in the parameter set.

Let's see how we can achieve method overloading:

public void SayHi()
{
    Console.WriteLine("Hi");
}

public void SayHi(string name)
{
    Console.WriteLine($"Hi {name}");
}
Enter fullscreen mode Exit fullscreen mode

Same name + Different parameters set = Different Signature.
Like that

Final Question

10: What's a static class ?

A static class solely contains static methods , properties , or in short , static members only , additionally , static classes are sealed , which implies they cannot be inherited , or used to create instances , an example of a static class that I mentioned multiple times previously , is the Console class , when you call Console.WriteLine(); , you don't actually create a new instance of the console , you're using the same console again and again , because no need arises to have instances of the console , and that's why static classes can be beneficial.

Checkpoint Reached

You've now 10 questions with their answers . Hooray 🎊
That's part 1 down , 1 more to go
Until then , keep learning , ACTIVELY

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up