{"id":15731702,"url":"https://github.com/kawser2133/programming-basics-part1","last_synced_at":"2025-03-31T02:53:03.880Z","repository":{"id":257140309,"uuid":"857424491","full_name":"kawser2133/programming-basics-part1","owner":"kawser2133","description":"Programming Basics Part 1 - This repository provides a comprehensive introduction to foundational C# programming concepts, including classes, objects, constructors, types of classes, and access modifiers.","archived":false,"fork":false,"pushed_at":"2024-09-29T15:17:38.000Z","size":35,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-06T08:11:10.792Z","etag":null,"topics":["access-modifier","classes","constructor","csharp","fields","methods","object-oriented-programming","objects","oop","programming","properties"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kawser2133.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-09-14T16:12:39.000Z","updated_at":"2024-09-29T15:17:41.000Z","dependencies_parsed_at":"2024-10-24T22:18:18.860Z","dependency_job_id":"bfbb6bc1-de22-4253-89f9-5bcc319c4080","html_url":"https://github.com/kawser2133/programming-basics-part1","commit_stats":{"total_commits":10,"total_committers":1,"mean_commits":10.0,"dds":0.0,"last_synced_commit":"323b5b902a10e560de7311cb3c015cee458e8d1d"},"previous_names":["kawser2133/programming-basics-part1"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kawser2133%2Fprogramming-basics-part1","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kawser2133%2Fprogramming-basics-part1/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kawser2133%2Fprogramming-basics-part1/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kawser2133%2Fprogramming-basics-part1/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kawser2133","download_url":"https://codeload.github.com/kawser2133/programming-basics-part1/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246408104,"owners_count":20772230,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["access-modifier","classes","constructor","csharp","fields","methods","object-oriented-programming","objects","oop","programming","properties"],"created_at":"2024-10-04T00:04:20.750Z","updated_at":"2025-03-31T02:53:03.857Z","avatar_url":"https://github.com/kawser2133.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# **Programming Basics Part 1**\n\nWelcome to **Programming Basics Part 1**, a repository focused on building foundational knowledge of C# programming concepts. \n\n### Sections Covered:\n\n1. [**Classes and Objects**](#1-classes-and-objects)\n   - [Introduction to Classes](#11-introduction-to-classes)\n   - [Fields, Properties, Methods](#12-fields-properties-and-methods)\n   - [Constructors](#13-constructors)\n   - [Types of Classes](#14-types-of-classes) (Static, Partial, Sealed, Abstract, Nested)\n\n2. [**Access Modifiers**](#2-access-modifiers)\n   - [Public, Private, Protected, Internal, Protected Internal](#summary-of-access-modifiers)\n\nThis repository aims to serve as a solid starting point for those interested in C# programming, providing practical examples and use cases for every concept discussed.\n\n\n## 1. Classes and Objects\n\n### 1.1 Introduction to Classes\n\nA **class** in C# is a blueprint or template for creating objects. It defines the structure of an object, including its properties (data), methods (behavior), and constructors (initialization logic). You can think of a class as a definition, while an object is an instance of that class, representing a specific entity.\n\n### Example:\n\n```csharp\n// Define a class named Car\npublic class Car\n{\n    // Fields (variables)\n    private string make;\n    private string model;\n\n    // Constructor\n    public Car(string make, string model)\n    {\n        this.make = make;\n        this.model = model;\n    }\n\n    // Methods\n    public void Drive()\n    {\n        Console.WriteLine($\"{make} {model} is driving!\");\n    }\n}\n\n// Create an object of the Car class\nCar car1 = new Car(\"Toyota\", \"Corolla\");\ncar1.Drive();  // Output: Toyota Corolla is driving!\n```\n\n- **Class Definition**: The `Car` class is a blueprint that defines what a \"car\" looks like, including its `make`, `model`, and the ability to `Drive()`.\n- **Object Creation**: The `car1` object is an instance of the `Car` class.\n\n---\n\n### **1.2 Fields, Properties, and Methods**\n\n### **Fields**\n\nFields are variables that hold data for the object. They are usually private and are accessed via **properties** to protect data integrity.\n\n### Example:\n\n```csharp\npublic class Person\n{\n    // Field (Private variable)\n    private string name;\n    private int age;\n\n    // Constructor to initialize fields\n    public Person(string name, int age)\n    {\n        this.name = name;\n        this.age = age;\n    }\n}\n```\n\n### **Properties**\n\nProperties in C# allow controlled access to fields. They are used to read and write field values through `get` and `set` accessors.\n\n### Example:\n\n```csharp\npublic class Person\n{\n    // Field (Private variable)\n    private string name;\n\n    // Property to get and set the name\n    public string Name\n    {\n        get { return name; }\n        set { name = value; }\n    }\n}\n\nPerson person = new Person();\nperson.Name = \"Alice\";  // Set the name using property\nConsole.WriteLine(person.Name);  // Output: Alice\n```\n\n- **`get` Accessor**: Retrieves the value of the field.\n- **`set` Accessor**: Assigns a new value to the field.\n\n### **Methods**\n\nMethods define the actions or behaviors an object can perform. They can be public or private depending on whether they should be accessed from outside the class.\n\n### Example:\n\n```csharp\npublic class Calculator\n{\n    // Method to add two numbers\n    public int Add(int a, int b)\n    {\n        return a + b;\n    }\n}\n\n// Using the method\nCalculator calc = new Calculator();\nint result = calc.Add(5, 10);\nConsole.WriteLine(result);  // Output: 15\n```\n\n---\n\n### **1.3 Constructors**\n\nA **constructor** is a special method that is automatically called when an object is created. It initializes the object’s state by assigning initial values to fields or properties.\n\n### Types of Constructors:\n\n1. **Default Constructor**: No parameters.\n2. **Parameterized Constructor**: Takes parameters to initialize fields.\n3. **Static Constructor**: Initializes static members of a class.\n\n### Example of Parameterized Constructor:\n\n```csharp\npublic class Book\n{\n    // Fields\n    private string title;\n    private string author;\n\n    // Parameterized constructor\n    public Book(string title, string author)\n    {\n        this.title = title;\n        this.author = author;\n    }\n\n    // Method to display book details\n    public void GetDetails()\n    {\n        Console.WriteLine($\"Title: {title}, Author: {author}\");\n    }\n}\n\n// Create object using the constructor\nBook book1 = new Book(\"1984\", \"George Orwell\");\nbook1.GetDetails();  // Output: Title: 1984, Author: George Orwell\n```\n\n### Default Constructor Example:\n\n```csharp\npublic class Book\n{\n    private string title = \"Unknown\";\n\n    // Default constructor\n    public Book() { }\n\n    public void GetTitle()\n    {\n        Console.WriteLine($\"Title: {title}\");\n    }\n}\n\nBook book = new Book();\nbook.GetTitle();  // Output: Title: Unknown\n```\n\n### **Static Constructor**:\n\nA **static constructor** is used to initialize static members of a class. It is called once, before any instance of the class is created.\n\n```csharp\npublic class MathHelper\n{\n    public static double Pi;\n\n    // Static constructor\n    static MathHelper()\n    {\n        Pi = 3.14159;\n    }\n\n    public static double CalculateCircleArea(double radius)\n    {\n        return Pi * radius * radius;\n    }\n}\n\n// No need to create an instance\ndouble area = MathHelper.CalculateCircleArea(5);\nConsole.WriteLine(area);  // Output: 78.53975\n```\n\n---\n\n### **1.4 Types of Classes**\n\nC# provides several types of classes, each with its specific use cases:\n\n### 1. **Static Classes**\n\nA static class cannot be instantiated and is used to group related static methods and properties. All members of a static class must be static.\n\n### Example:\n\n```csharp\npublic static class MathHelper\n{\n    public static double Pi = 3.14159;\n\n    public static double CalculateCircleArea(double radius)\n    {\n        return Pi * radius * radius;\n    }\n}\n\n// Usage without instantiation\ndouble area = MathHelper.CalculateCircleArea(5);\nConsole.WriteLine(area);  // Output: 78.53975\n```\n\n- **Note**: You cannot create an object of a static class: `MathHelper helper = new MathHelper();` would result in an error.\n\n### 2. **Partial Classes**\n\nPartial classes allow splitting the definition of a class across multiple files. This is useful when working on large projects with many developers.\n\n### Example:\n\n```csharp\n// File: Car_Part1.cs\npublic partial class Car\n{\n    public string Make { get; set; }\n}\n\n// File: Car_Part2.cs\npublic partial class Car\n{\n    public string Model { get; set; }\n\n    public void DisplayDetails()\n    {\n        Console.WriteLine($\"{Make} {Model}\");\n    }\n}\n\n// Both files together define a complete Car class\nCar car = new Car();\ncar.Make = \"Toyota\";\ncar.Model = \"Corolla\";\ncar.DisplayDetails();  // Output: Toyota Corolla\n```\n\n### 3. **Sealed Classes**\n\nA sealed class cannot be inherited. It is used when you want to prevent other classes from deriving from it.\n\n### Example:\n\n```csharp\npublic sealed class FinalClass\n{\n    public void Display()\n    {\n        Console.WriteLine(\"This class cannot be inherited.\");\n    }\n}\n\n// Error: You cannot inherit from a sealed class\n// public class DerivedClass : FinalClass { }\n```\n\n### 4. **Abstract Classes**\n\nAn abstract class cannot be instantiated directly and may contain abstract methods (without implementation) that must be implemented by derived classes. It can also contain fully implemented methods.\n\n### Example:\n\n```csharp\npublic abstract class Animal\n{\n    // Abstract method (no implementation)\n    public abstract void MakeSound();\n\n    // Concrete method\n    public void Sleep()\n    {\n        Console.WriteLine(\"The animal is sleeping.\");\n    }\n}\n\npublic class Dog : Animal\n{\n    // Providing implementation for the abstract method\n    public override void MakeSound()\n    {\n        Console.WriteLine(\"Dog barks.\");\n    }\n}\n\nDog dog = new Dog();\ndog.MakeSound();  // Output: Dog barks.\ndog.Sleep();      // Output: The animal is sleeping.\n```\n\n### 5. **Nested Classes**\n\nA class declared inside another class is called a nested class. It is used when the relationship between the two classes is very close.\n\n### Example:\n\n```csharp\npublic class OuterClass\n{\n    public class NestedClass\n    {\n        public void ShowMessage()\n        {\n            Console.WriteLine(\"This is a nested class.\");\n        }\n    }\n}\n\n// Usage\nOuterClass.NestedClass nested = new OuterClass.NestedClass();\nnested.ShowMessage();  // Output: This is a nested class.\n```\n---\n\n## 2. Access Modifiers\n\nAccess modifiers in C# control the visibility and accessibility of classes, methods, properties, and other members in a program. Here's a detailed explanation of the most common access modifiers:\n\n### 1. **Public**\n\n- **Definition**: Accessible from anywhere in the code.\n- **Use Case**: Use `public` when you want a member to be accessible from other classes and projects.\n\n### Example:\n\n```csharp\npublic class Person\n{\n    public string Name;\n\n    public void Greet()\n    {\n        Console.WriteLine($\"Hello, my name is {Name}\");\n    }\n}\n\nclass Program\n{\n    static void Main()\n    {\n        Person person = new Person();\n        person.Name = \"John\"; // Accessible because 'Name' is public\n        person.Greet();       // Accessible because 'Greet()' is public\n    }\n}\n```\n\n- **Explanation**: The `Person` class has a public field `Name` and a public method `Greet()`. Both are accessible from outside the class, as seen in the `Main()` method.\n\n---\n\n### 2. **Private**\n\n- **Definition**: Accessible only within the class in which it is declared.\n- **Use Case**: Use `private` to hide internal details of a class, keeping certain members inaccessible to other parts of the program.\n\n### Example:\n\n```csharp\npublic class Person\n{\n    private string Name;  // Private field, accessible only within the class\n\n    public void SetName(string name)\n    {\n        Name = name;\n    }\n\n    public void Greet()\n    {\n        Console.WriteLine($\"Hello, my name is {Name}\");\n    }\n}\n\nclass Program\n{\n    static void Main()\n    {\n        Person person = new Person();\n        person.SetName(\"John\");  // You can call the public method to set 'Name'\n        person.Greet();          // Accessing the private 'Name' indirectly\n    }\n}\n```\n\n- **Explanation**: The `Name` field is private, meaning it can't be accessed directly outside of the `Person` class. Instead, you use the public `SetName()` method to set its value, ensuring encapsulation.\n\n---\n\n### 3. **Protected**\n\n- **Definition**: Accessible within its own class and by derived classes (through inheritance).\n- **Use Case**: Use `protected` when you want a member to be available only to the class itself and any class that inherits from it.\n\n### Example:\n\n```csharp\npublic class Person\n{\n    protected string Name;  // Protected, accessible to derived classes\n\n    public void Greet()\n    {\n        Console.WriteLine($\"Hello, my name is {Name}\");\n    }\n}\n\npublic class Employee : Person\n{\n    public void SetEmployeeName(string employeeName)\n    {\n        Name = employeeName;  // Accessible because 'Employee' inherits from 'Person'\n    }\n}\n\nclass Program\n{\n    static void Main()\n    {\n        Employee employee = new Employee();\n        employee.SetEmployeeName(\"John\");\n        employee.Greet();\n    }\n}\n```\n\n- **Explanation**: The `Name` field is `protected`, so it cannot be accessed directly from outside, but it can be accessed by any class that inherits from `Person` (like `Employee`).\n\n---\n\n### 4. **Internal**\n\n- **Definition**: Accessible only within the same assembly (project). Other assemblies cannot access it.\n- **Use Case**: Use `internal` when you want members to be accessible within the same project but hide them from other projects.\n\n### Example:\n\n```csharp\ninternal class InternalClass\n{\n    public void DisplayMessage()\n    {\n        Console.WriteLine(\"This is an internal class.\");\n    }\n}\n\nclass Program\n{\n    static void Main()\n    {\n        InternalClass internalClass = new InternalClass();\n        internalClass.DisplayMessage();\n    }\n}\n```\n\n- **Explanation**: The `InternalClass` can be accessed anywhere within the same project, but if you reference this project from another, `InternalClass` will not be available.\n\n---\n\n### 5. **Protected Internal**\n\n- **Definition**: Accessible within its own assembly or by derived classes (even if they are in a different assembly).\n- **Use Case**: Use `protected internal` when you want members to be accessible both within the assembly and in derived classes outside the assembly.\n\n### Example:\n\n```csharp\npublic class Person\n{\n    protected internal string Name;  // Accessible in the same assembly or derived classes in other assemblies\n}\n\nclass Program\n{\n    static void Main()\n    {\n        Person person = new Person();\n        person.Name = \"John\";  // Accessible because it's in the same assembly\n        Console.WriteLine(person.Name);\n    }\n}\n```\n\n- **Explanation**: The `Name` field is marked as `protected internal`, so it can be accessed anywhere within the same assembly and also by derived classes, even in different assemblies.\n\n---\n\n### Summary of Access Modifiers:\n\n| Modifier | Same Class | Derived Class (Same Assembly) | Derived Class (Other Assembly) | Same Assembly (Non-derived) | Other Assembly |\n| --- | --- | --- | --- | --- | --- |\n| **public** | Yes | Yes | Yes | Yes | Yes |\n| **private** | Yes | No | No | No | No |\n| **protected** | Yes | Yes | Yes | No | No |\n| **internal** | Yes | Yes | No | Yes | No |\n| **protected internal** | Yes | Yes | Yes | Yes | No |\n\n---\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkawser2133%2Fprogramming-basics-part1","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkawser2133%2Fprogramming-basics-part1","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkawser2133%2Fprogramming-basics-part1/lists"}