Creating Using Objects Notes
Creating Using Objects Notes
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 .