Code Monkey home page Code Monkey logo

csharppractice's Introduction

File Manager Console Application

Development using .NET 6 platform, C# 10 Language Version

Reference: Microsoft Learn - for educational purpose, CodeMaze, Blog

Example For Some Fundamentals C# Concepts/.NET features

Partial Class

Make it possible to split the definition of a class, a struct, an interface or a method in many files.

    public static partial class Options
    {
        ...

Together with the namespace system, this aid code structuring and refactoring more comfortable even for many people working on same codebase.

LinQ

LINQ - Language Integrated Query is the integration of query capabilities directly into the C# language.

    return rootList
                .SelectMany(
                    root => FlattenRootsByValue(root.Childs, value)
                ).Concat(
                    rootList.Where(root => root.Value == value)
                );
...
    return (from child in Childs select child.Value);

With LINQ, type checking at compile time or IntelliSense support is added, and query is first-class language construct, like classes, methods, events.

Delegates

Delegates provide late binding mechanism in .NET == an procedure where the caller supplies a part of the procedure.

    public delegate void EnterKeyEventHandler (object sender, EnterKeyEventArgs eArgs);
    public delegate void SpaceKeyEventHandler (object sender, SpaceKeyEventArgs eArgs);
...
    // public delegate int Comparison<in T>(T x, T y); -- predefined in System namespace
...
    public static List<T> Sort(IEnumerable<T> enumerable, Comparison<T> comparatorForT)
...
    EnumerableSorter<Cocktail>.Sort(
       getCocktaiListFromJson(responseContent), cocktailCompareByNameLength
    )

Multicast Delegate

Multiple function object can be assigned to one using the +, += operator to form a list of the assigned delegates.

When called, it invokes the chain of delegates in order. Only delegates of same type can be combined.

Generics

Like other languages, Generic introduce concept of type parameter to .NET. It can be applied to Classes, Methods, Types.

 class EnumerablePrinter<T>   
    {
        public static void printEnumerableByLine(IEnumerable<T> enummerable)
        {
            Console.WriteLine("[");

            foreach (T item in enummerable)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("]");
        }

Literally, with Generics, we can define a class that defer the the parameter specifying to client code without involving runtime casting or boxing.

Bounded Generics

    public class EquationFactory<T1, T2>
        where T1 : System.ValueTuple<int, int, int, string>
        where T2 : QuadraticEquation
...

Events

Events enable a class or object to notify other classes or objects when something of interest occurs. It provide more official way to implement Pattern: Publisher - Subscriber

In a typical C# application, you subscribe to events raised by buttons and list boxes.

class EnterKeyEventPublisher : KeyEventPublisher
    {
        public event EnterKeyEventHandler KeyEvent;

        protected virtual void OnKeyPressed(EnterKeyEventArgs eArgs)
...
 class SpaceKeyEventPublisher : KeyEventPublisher
    {
        public event SpaceKeyEventHandler KeyEvent;

        protected virtual void OnKeyPressed(SpaceKeyEventArgs eArgs)
...

Extension Method

We can add methods to types without creating new derived type, recompiling, or modifying original type.

    public static string Name(this string filePath)
    {...}
...

Extension methods are static methods, but they can be called as if they were instance methods. Some methods from LinQ GroupBy, OrderBy, Average from IEnumerable interface are example.

Tuple (> C# 7)

...
      var equation = EquationFactory<
          (int, int, int, string),
          QuadraticEquation
      >.CreateWith4Args( (1, 2, 1, "x");

Object Initializer

    public static T2 CreateWith2Args(T1 dataTuple)
    {
        return new T2{
            A = dataTuple.Item1,
            B = dataTuple.Item2,
            C = 0,
            Variable = "x"
     };

Nullable Types

...
    public class UnitOfWork : IUnitOfWork
    {
        private ITransaction? _currentTransaction;

        public UnitOfWork(IDatabase database)
...
Dictionary<K, V> vs Hashtable ??
  • Dictionary<K, V> provide generics and type-saftety on element, avoid casting and random object

In fact, generic Dictionary was a copy of Hashtable - MS Reference

This console application was created for the purpose of learning the C# language. It enable user with some(and maybe more) functionalities:

  • View a table of all files in a directory (including file size and last access timestamp).
  • View a table of all subfolders in a directory (pretty much the same idea as above).
  • Create, delete, move, rename, read text from and write text to files, as well as searching text files for specific phrases.
  • Create, delete, move and rename folders.
  • Create and save an 'index.txt' file, which displays all of the files and subfolders in a directory, with some useful information (basically the same thing as the first 2 bullet points, but in a HANDY and CONVENIENT text file).
  • Colorful console animation!

csharppractice's People

Contributors

trungisongithhub avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.