AWP
AWP
Ans:-
A namespace in C# is a way to organize and manage code, especially in
larger projects, by grouping related classes, interfaces, structs, enums,
and delegates together. Think of it as a container or a folder that helps
avoid naming conflicts and makes code more readable and
maintainable.
1. Purpose of Namespaces:
● Organization: They help organize code logically, making it easier to
manage, especially in large projects.
● Avoiding Naming Conflicts: Different classes can have the same
name as long as they belong to different namespaces.
2. Syntax:
namespace MyNamespace
{
public class MyClass
{
// class members
}
}
3. Using Namespaces:
● To use a class from another namespace, you typically include a
using directive at the top of your file:
using MyNamespace;
4. Nested Namespaces:
● Namespaces can be nested, allowing for further organization
namespace OuterNamespace
{
namespace InnerNamespace
{
public class InnerClass
{
// class members
}
}
}
5. Global Namespace:
● The global namespace is the root namespace. If you want to
ensure you are referring to something in the global namespace,
you can use the global:: prefix.
6. Partial Classes and Namespaces:
● You can split a class definition across multiple files, and as long as
they are in the same namespace, they will be compiled as a single
class.
using MyApp.Logging;
using System.Logging;
// Derived class
public class Dog : Animal
{
public void Bark()
{
Console.WriteLine("Barking...");
}
}
● Explanation: Here, Dog is the derived class that inherits from the
Animal base class. This means Dog has access to the Eat() method
defined in Animal, in addition to its own Bark() method.
2. Multilevel Inheritance:
● Definition: In multilevel inheritance, a class is derived from another
class, which is itself derived from another class. This creates a
chain of inheritance.
● Example:
// Base class
public class Animal
{
public void Eat()
{
Console.WriteLine("Eating...");
}
}
// Derived class
public class Mammal : Animal
{
public void Breathe()
{
Console.WriteLine("Breathing...");
}
}
Components of a Method:
1. Access Modifier:
○ Definition: Determines the visibility of the method to other
classes.
○ Common Modifiers:
■ public: The method can be accessed from any other
class.
■ private: The method can only be accessed within the
class it is defined.
■ protected: The method can be accessed within its class
and by derived classes.
■ internal: The method can be accessed within the same
assembly.
2. Return Type:
○ Definition: Specifies the type of value the method returns.
○ Examples:
■ void: The method does not return a value.
■ int: The method returns an integer.
■ string: The method returns a string.
3. Method Name:
○ Definition: The name of the method, which should be
descriptive of its functionality.
○ Naming Convention: Typically written in PascalCase.
4. Parameter List:
○ Definition: A comma-separated list of parameters that the
method takes as input.
○ Syntax: Each parameter is defined by a type and a name, e.g.,
int number.
○ Optional Parameters: You can also define default values for
parameters.
5. Method Body:
○ Definition: The block of code inside the curly braces {} that
defines what the method does.
○ Example: This is where you write the logic that should be
executed when the method is called.
Example of a Method:
public int AddNumbers(int a, int b)
{
int result = a + b;
return result; // This returns the sum of a and b
}
Q4. What is Typecasting & Explain its types?
Ans:-
Type casting in C# is the process of converting a variable from one data
type to another. It is necessary when you need to assign a value of one
type to a variable of another type, especially when those types are not
automatically compatible.
Types of Type Casting:
1. Implicit Casting (Automatic Type Conversion):
○ Definition: This happens automatically when converting a
smaller data type to a larger data type. No special syntax is
needed because there is no risk of data loss.
○ Example:
int num = 10;
double decimalNum = num; // Implicit casting from int to double
public string Name // Public property to access and change the name
{
get { return name; }
set { name = value; }
}
2. Inheritance
● What It Is: Inheritance lets you create a new class based on an
existing class. The new class inherits the features (methods and
properties) of the old class.
● Why It’s Useful: Reuse code and build on existing functionality.
● Example:
public class Animal
{
public void Eat()
{
Console.WriteLine("Eating...");
}
}
3. Polymorphism
● What It Is: Polymorphism allows you to use a single name for
different types of operations. It lets one method do different things
based on what it's acting on.
● Why It’s Useful: Makes your code more flexible and easier to
manage.
● Example:
○ Method Overloading: Different methods with the same name
but different parameters.
public class Calculator
{
public int Add(int a, int b)
{
return a + b;
}
4. Abstraction
● What It Is: Abstraction hides complex details and shows only the
essential features. It’s like using a TV remote without knowing how
it works inside.
● Why It’s Useful: Simplifies how you interact with complex systems.
● Example:
public abstract class Shape
{
public abstract void Draw(); // No implementation here
}