Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Array ^

The ^ operator is used for indexing from the end of an array or collection. Specifically, ^1 represents the last element, ^2 represents the second-to-last element, 


  void Display(int[] s) => Console.WriteLine(string.Join(" ", s));
  int[] xs = [0, 0, 0];
Display(xs);

ref int element = ref xs[0];
element = 1;
Display(xs);

element = ref xs[^1];
element = 3;
Display(xs);
// Output:
// 0 0 0
// 1 0 0
// 1 0 3
Refereneces:
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/declarations#reference-variables

Reference types vs Value types

Both value types and reference types are used to represent data, but they behave differently in terms of memory allocation, assignment, and passing to methods.

Memory Allocation:
Value Types:
Value types are stored directly in memory where they are declared. They typically reside on the stack. Examples of value types include simple types like int, float, char, and structs.

Reference Types:
Reference types are stored in memory on the heap.
When you declare a reference type variable, the variable itself stores a reference (or memory address) to the actual data on the heap.
Examples of reference types include classes, arrays, and interfaces.

Assignment:
Value Types:
When you assign a value type variable to another variable, a copy of the value is created.
Modifications to one variable do not affect the other.

Reference Types:
When you assign a reference type variable to another variable, you're actually copying the reference to the same underlying object in memory.
Both variables will refer to the same object, so changes made through one variable will be reflected when accessing the object through the other variable.

Passing to Methods:
Value Types:
When you pass a value type variable to a method, a copy of the variable's value is passed.
Any modifications made to the parameter inside the method do not affect the original variable.

Reference Types:
When you pass a reference type variable to a method, you're passing the reference to the same object in memory.
Therefore, changes made to the object inside the method will affect the object outside the method.

Nullability:
Value Types:
Value types cannot be null unless they are nullable value types, which are declared using the ? modifier (e.g., int?, float?).
Nullable value types can represent the value of the underlying type or a null value.

Reference Types:
Reference types can be null by default. If a reference type variable is not initialized, it will point to null, indicating that it does not reference any object in memory.

Sealed Class

The sealed keyword is used to prevent a class from being inherited or to prevent a method from being overridden.
When a class is marked as sealed, it means that it cannot be used as a base class for further inheritance.
Similarly, when a method is marked as sealed, it means that it cannot be overridden in derived classes.

Sealed Class:

public sealed class FinalClass
{
    // Class members and methods
}

// This will cause a compile-time error
public class DerivedClass : FinalClass
{
    // Cannot inherit from a sealed class
}

Sealed Method:

public class BaseClass
{
    public virtual void Method1()
    {
        Console.WriteLine("Method1 in BaseClass");
    }

    public sealed void Method2()
    {
        Console.WriteLine("Method2 in BaseClass");
    }
}

public class DerivedClass : BaseClass
{
    // This will cause a compile-time error
    public override void Method1()
    {
        Console.WriteLine("Method1 in DerivedClass");
    }
}

Exceptions

 https://www.tutorialsteacher.com/csharp/csharp-exception

 https://blog.naver.com/PostView.naver?blogId=coding-abc&logNo=222388017000&parentCategoryNo=&categoryNo=&viewDate=&isShowPopularPosts=false&from=postView