0% found this document useful (0 votes)
6 views4 pages

Creating Using Objects Notes

The document provides an overview of creating and using objects in C#, including the definition and purpose of namespaces, common namespaces, and the syntax for declaring them. It explains class creation as a blueprint for objects, detailing how to create objects, assign values, and call methods. A complete example demonstrates the process of creating a 'Student' class, instantiating an object, and displaying its properties.

Uploaded by

dpkyasoft
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Creating Using Objects Notes

The document provides an overview of creating and using objects in C#, including the definition and purpose of namespaces, common namespaces, and the syntax for declaring them. It explains class creation as a blueprint for objects, detailing how to create objects, assign values, and call methods. A complete example demonstrates the process of creating a 'Student' class, instantiating an object, and displaying its properties.

Uploaded by

dpkyasoft
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Creating and Using Objects in C#

1. Namespace in C#

A namespace is a container that holds classes, interfaces, structs, and other namespaces. It
helps avoid name conflicts and organizes code.

2. Common Namespaces in C#

- System

- [Link]

- [Link]

- [Link]

- [Link]

- [Link]

- [Link]

- [Link]

3. Declaring Namespaces

Syntax 1:

namespace MyNamespace

class Test { }

Syntax 2 (Nested Namespace):

namespace A.B.C

{
class Demo { }

Syntax 3 (Multiple Namespaces in Same File):

namespace Project1 { class A { } }

namespace Project2 { class B { } }

4. Class Creation

A class is a blueprint that contains fields (variables), methods (functions), properties, and
constructors.

Syntax:

class ClassName

// fields

// methods

Example:

class Student

public string Name;

public int Age;

public void Display()

[Link](Name + " " + Age);


}

5. Creating and Using Objects

Objects are instances of classes. They store data and provide functionality.

Creating Object:

Student s = new Student();

Assigning Values:

[Link] = "Arun";

[Link] = 20;

Calling Method:

[Link]();

Complete Example:

using System;

namespace ObjectExample

class Student

public string Name;

public int Age;


public void Display()

[Link]("Name: " + Name);

[Link]("Age: " + Age);

class Program

static void Main()

Student s1 = new Student();

[Link] = "Arun";

[Link] = 20;

[Link]();

Output:

Name: Arun

Age: 20

Common questions

Powered by AI

A class in C# is defined using the 'class' keyword followed by the class name and curly braces to encapsulate its body. Inside, it contains data members (fields), methods, properties, and constructors. Methods are functions defined within the class scope, used to perform operations. Example: 'class Student { public string Name; public int Age; public void Display() { Console.WriteLine("Name: " + Name); } }' .

Multiple namespaces can be declared within a single C# file using separate namespace blocks, like: 'namespace Project1 { class A { } } namespace Project2 { class B { } }'. This approach allows for organizing code into separate logical units within the same file, improving modularity and clarity without the need for additional files .

In C#, classes and methods are named using PascalCase, where each word is capitalized. Classes typically represent entities, hence naming should be nouns, e.g., 'Student', and methods should be verbs, e.g., 'GetData'. These conventions provide clarity, making code self-descriptive, improving readability by indicating purpose, and enhancing maintainability by standardizing the approach across different codebases .

Organizing code into different namespaces enhances maintainability and scalability by containing potential naming conflicts, grouping related classes and logic, and simplifying navigation in large codebases. It allows developers to understand and modify sections of code independently, facilitating parallel development and reducing the risk of errors. Good namespace management is critical for structuring complex applications and ensuring that they remain efficient and logical to navigate .

Constructors in C# are special methods that are automatically called when an object is instantiated. They initialize object properties, allowing parameters to be passed into the new instance. Without explicit definition, a default constructor is provided, but custom constructors can be defined to set initial property values or run logic during object creation .

A nested namespace in C# is structured using a hierarchical format where one namespace is declared inside another. The syntax resembles: 'namespace A.B.C { class Demo { } }'. This allows for hierarchical organization of code, especially useful for logically grouping related functionality .

A namespace in C# is a container that organizes classes, interfaces, structs, and other namespaces, helping to avoid name conflicts by providing a structured way to manage and group code elements. It allows developers to manage the scope of their classes and methods within larger projects, ensuring that naming collisions are prevented and code remains modular and maintainable .

Example: 'class Calculator { public int Add(int a, int b) { return a + b; } }'. Components: 'class Calculator' defines a blueprint. 'public int Add(int a, int b)' is a method specifying the operation and parameters. The method is accessed by creating an instance: 'Calculator calc = new Calculator(); calc.Add(3, 5);'. Here, 'calc.Add' calls the method on the instance, performing the defined action (addition).

Object instantiation is fundamental in C# programming as it makes abstract class definitions concrete and usable. Through instantiation, objects are created that encapsulate data and behavior, allowing interaction with these objects via defined interfaces (methods). This encapsulation and interaction are cornerstones of object-oriented programming, promoting code reuse, abstraction, and a structured approach to complex problem-solving .

Creating and using objects in C# involves defining a class as a blueprint containing fields, methods, and properties. For instance, declare a class 'Student' with fields 'Name' and 'Age', and a method 'Display()'. Instantiate the class using 'Student s = new Student();', assign values to its fields like 's.Name = "Arun";' and 's.Age = 20;', and interact with it by calling its methods such as 's.Display();', which will use the object's data .

You might also like