Tuesday, 13 August 2019

14


14:

DataContext Class: Replace SQL Connection

Table Class: Reflects all fields in the database table.

16


Solah:

Data Island should be place logically in Window.Resources, however, it can be created anywhere in the page. Data island ties the names on the screen with the namespace in code and will be used to code to access it.

Pandrah


Pandrah

If we save 12345 in a text file as a string then as the number will be saved as String and because Character is 1 byte long in C#.

Therefore, 12345 when stored in the text file, it will at least use 5 Bytes if space.

Whereas, if the number is stored binary file then the number will be stored in binary format.

(12345) ==> 0b 00110000 00111001 ==> 2 Bytes.


Tera: Tera Tera Saroor



Tera:

When LINQ is used with Non-Generic collections such as ArrayList, HashTable, etc, We have to use <cast> in to order to cast the Non-Generic Collection to Generic Collection.

LINQ is not very good with Non-Generics therefore, we use CAST to overcome.

Barrah


BARAH

In C#, Validation class needs to be added in the code that allow to to handle the error by registering an error handler.

Validation.AddErrorHandler(textBoxName, textBox_ValidationError); this method is called within MainWindow()

protected void textBox_ValidationError(object sender, ValidationErrorEventArgs error){
MessageBox.Show(string)error.Error.ErroContent);
}

When an error occurred, the messageBox will show from textBox_ValidationError method and it will notify the user.

In XAML, the error handling is done using Notification window, when an error occurred error notification window will show on the scree.

<TextBox.Text>
<Binding Path = "FieldName"
NotifyValidationError = "True">
</Binding>
</TextBox.Text>

Above, when the data input is invalid, error will be notified.

GYAARAH


GYAARAH:



There is no significant difference between the performance of lambda and non-lambda LINQ queries.

In addition, after compilation, both LINQ and LAMBDA query results in the same file which is then executed at compile time.


Choosing between LINQ and LAMBDA is a personal preference.

Dus: DUS BAHANE KER KE LE GAYA DIL



DUS:

Boolean data type is replaced by checkbox in the grids. Example:
<DataGridCheckBoxColumn Header="Consent?" Binding="{Binding ConsentFlag}" />

No: Directory Management in C#




Nine:

If the target directory has any subdirectory(ies) in it then Directory.Delete() will throw an exception that "Directory is not empty".

Fix:

1. Traverse from the bottom to top of the target directory path.

2. In order to Traverse, we need to construct the absolute path of the target directory and its subdirectories.

3. Once we have the absolute path, we can enumerate the directories and perform Deletion one by one.

AATH: AUto Generataion



  1. DataTable dt = new DataTable();
  2. dt.Columns.Add("Column1", typeof(String)); // First col is a string
  3. dt.Columns.Add("Column2", typeof(Boolean)); // Second col is a boolean
  4. DataRow dr = dt.NewRow();
  5. dr["Column1"] = "Data";
  6. dr["Column2"] = true;
  7. dt.Rows.Add(dr);
  8. dataGridView1.DataSource = dt;



AATH:

The AutoGenerateColumns property of DataGrid should set to [AutoGenerateColumns="false"] when creating the dataGrid and customized columns should be define as per the requirement.

SAAT: Data Grid and Data Island


Operator overloadability

The null-coalescing operator cannot be overloaded.

C# language specification

For more information, see The null coalescing operator section of the C# language specification.


SAAT:

Data Binding in WPF used to connect the flow of data between UI and the code. Binding can be performed in two different ways:
1. Data Island - creating data in the immediate window
2. The data source is used to retrieve data and then use in the window.

Class is required in both cases that contains the definition of data.

Cheh: Data Binding in C#

  • In expressions with the null-conditional operators ?. and ?[], you can use the null-coalescing operator to provide an alternative expression to evaluate in case the result of the expression with null-conditional operations is null:
    C#
    double SumNumbers(List<double[]> setsOfNumbers, int indexOfSetToSum)
    {
        return setsOfNumbers?[indexOfSetToSum]?.Sum() ?? double.NaN;
    }
    
    var sum = SumNumbers(null, 0);
    Console.WriteLine(sum);  // output: NaN
CHEH:

LIFO(Last in First Out): Stack Collection we will use.

Paanch: Collections in C#



The null-coalescing operator ?? returns the value of its left-hand operand if it isn't null; otherwise, it evaluates the right-hand operand and returns its result. The ?? operator doesn't evaluate its right-hand operand if the left-hand operand evaluates to non-null.
The null-coalescing operator is right-associative, that is, an expression of the form
C#
a ?? b ?? c
is evaluated as
C#
a ?? (b ?? c)
The ?? operator can be useful in the following scenarios:



Paanch:

Peek -- points to an oldest element in the queue without removing it.

Example:

Queue<string> myQueue = new Queue<string>();

myQueue.Enqueue("1");
myQueue.Enqueue("2")

Then,

myQueue.Peek(); //will result "1"

Chaar: WPF Programming and C#






Adding with Kristin's answer, There is no difference in performance because after compile they generate same IL code for the same query. Language Integrated Query (LINQ) is feature of Visual Studio that gives you the capabilities yo query on the language syntax of C#, so you will get SQL kind of queries. And Lambda expression is an anonymous function and is more of a like delegate type. All lambda expressions use the lambda operator =>, which is read as "goes to". The left side of the lambda operator specifies the input parameters (if any) and the right side holds the expression or statement block. Personally I like Lambda Expression. Hope this helps you.


Chaar:

XMLSerializer class when used together with a Text Writer allows you to store C# structure to XML file but we lose the top element of the generic array collection.

To avoid this, serialization can be improved by using XmlRoot attribute. Example:

List<Person> personList = FormTouristList();
XmlSerializer xmlSerializer = new XmlSerializer(List<Person>);

[XmlRoot("PersonList")] can be used while serializing to avoid losing the top element.

DO: XML Serialisation



xml tree - sibling

A sibling relationship exists when two elements have the same parent. There are no brothers or sisters in XML, only siblings.

finding your siblings

To find siblings in an XML document, look for elements that are on the same level of the tree and have the same parent.
Who is the sibling of snack?

XML Code, lemonade.xml:

<inventory>
 <drink>
  <lemonade>
   <price>$2.50</price>
   <amount>20</amount>
  </lemonade>
  <pop>
   <price>$1.50</price>
   <amount>10</amount>
  </pop>
 </drink>
 
 <snack>
  <chips>
   <price>$4.50</price>
   <amount>60</amount>
  </chips>
 </snack>
</inventory>
drink is the sibling of snack.
If you look at the tree structure, you can clearly see that the elements drink and snack, of the lemonade.xml document, share the same parent, inventory.

XML Tree:






DO:

To access WPF controls in code, the controls element need to set to x:Code directive. Example, to access Register button in code, x:Name should be included in the code.

<Button x:Name="btnRegister" Content="Register Appointment" HorizontalAlignment="Left" Click="btnRegister_Click"/>

Ek: Collections in C#


C# (pronounced "See Sharp") is a simple, modern, object-oriented, and type-safe programming language. C# has its roots in the C family of languages and will be immediately familiar to C, C++, Java, and JavaScript programmers. This tour provides an overview of the major components of the language. If you want to explore the language through interactive examples, try our introduction to C#tutorials.
C# is an object-oriented language, but C# further includes support for component-oriented programming. Contemporary software design increasingly relies on software components in the form of self-contained and self-describing packages of functionality. Key to such components is that they present a programming model with properties, methods, and events; they have attributes that provide declarative information about the component; and they incorporate their own documentation. C# provides language constructs to support directly these concepts, making C# a very natural language in which to create and use software components.
Several C# features aid in the construction of robust and durable applications: Garbage collection automatically reclaims memory occupied by unreachable unused objects; exception handling provides a structured and extensible approach to error detection and recovery; and the type-safe design of the language makes it impossible to read from uninitialized variables to index arrays beyond their bounds, or to perform unchecked type casts.
C# has a unified type system. All C# types, including primitive types such as int and double, inherit from a single root object type. Thus, all types share a set of common operations, and values of any type can be stored, transported, and operated upon in a consistent manner. Furthermore, C# supports both user-defined reference types and value types, allowing dynamic allocation of objects as well as in-line storage of lightweight structures.



Ek:

It's a dynamic collection of objects of a given type. It allows programmers to receive changes to collections example add, remove or update objects without an additional refresh to UI.

Example: Window with a Button, two TextBoxes and a ListView, clicking the button will add the text of the textBox to the Collection and the ListView is updated automatically.

private ObservableCollection<Object> demoCollection;

public MainWindow(){

demoCollection;= new ObservableCollection<Object>()
}

Teen: C# XMLManagement

The C# guide provides many resources about the C# language. This site has many different audiences. Depending on your experience with programming, or with the C# language and .NET, you may wish to explore different sections of this guide.
  • For brand-new developers:
    • Start with the Introduction to C# tutorials. These tutorials let you explore the C# language interactively in your browser. From there, you can move on to other tutorials. These tutorials show you how to create C# programs from scratch. The tutorials provide a step-by-step process to create programs. They show the language concepts and how to build C# programs on your own. If you prefer reading overview information first, try the tour of the C# language. It explains the concepts of the C# language. After reading this, you'll have a basic understanding of the language, and be ready to try the tutorials, or build something on your own.
  • For developers new to C#:
    • If you've done development before, but are new to C#, read the tour of the C# language. It covers the basic syntax and structure for the language, and you can use the language tour to contrast C# with other languages you've used. You can also browse the tutorials to try basic C# programs.
  • Experienced C# developers:
    • If you've used C# before, you should start by reading what's in the latest version of the language. Check out What's new in C# for the new features in the current version.

Teen:

Y is a DESCENDANT of X.
Z is a SIBLING of X

C# Keywords

keywords are predefined, reserved identifiers that have special meanings to the compiler. They cannot be used as identifiers in your program unless they include @ as a prefix. For example, @if is a valid identifier, but if is not because if is a keyword.
The first table in this topic lists keywords that are reserved identifiers in any part of a C# program. The second table in this topic lists the contextual keywords in C#. Contextual keywords have special meaning only in a limited program context and can be used as identifiers outside that context. Generally, as new keywords are added to the C# language, they are added as contextual keywords in order to avoid breaking programs written in earlier versions.




Monday, 12 August 2019

ArrayList can accept instances of any class or value type

Using File Stream Class is very similar to using File.Open() method.

The instance of File Stream Class will be exactly the same.


• In our case, we are placing data from XML back into the list
• It is good to specify the settings when you read so that
the comments and whitespaces are not recognized
• In the reading process, we go straight to the first element and keep reading until all tourists are gone
• The last thing to do is to close
the reader



FileStream fs = new FileStream("text.xml")

C# Writing into Binary File

Binary Files are written using BinaryWriter.



  • Every XML element is allowed to have a text,
  • enclosed between opening and closing tags. This
  • text is called tag data <name>Igor</

  • The data can also be stored inside the opening
  • tag. This data is called “attribute” of the tag
  • <name type=“first”>Igor</
  • The attributes can ONLY be stored in the
  • an opening tag and NEVER in the closing tag

C# File Management

This is about C# file Management.

CTS Reservation System has following highlights:
1. Age wise sorting of record
2. Highlighting of Search Record
3. Search Records based on four paramters




Sunday, 11 August 2019

How to Handle File and Directories in C#?

FileStream fs = null;

string var = @".\text.txt";

if(File.Exists(var)) {
 return null
}

Queue in C#

Queue Data Structure is an collection artifacte. A8: Abstract Class

Abstract class provides superpower to C#

Collections in C#

Collections in C#

Collections play a large role in the development of the applications.

We already used a few collections, such as List<> and ArrayList


Delegate declaration determines the methods that can be referenced by the delegate. A delegate can refer to a method, which has the same signature as that of the delegate.
For example, consider a delegate −
public delegate int MyDelegate (string s);
The preceding delegate can be used to reference any method that has a single string parameter and returns an int type variable.

Instantiating Delegates

Once a delegate type is declared, a delegate object must be created with the new keyword and be associated with a particular method. When creating a delegate, the argument passed to the newexpression is written similar to a method call, but without the arguments to the method. For example −
public delegate void printString(string s);
...
printString ps1 = new printString(WriteToScreen);
printString ps2 = new printString(WriteToFile);
Following example demonstrates declaration, instantiation, and use of a delegate that can be used to reference methods that take an integer parameter and returns an integer value.

Directory and File Management in C#


C# supports Directory and File Management. C# and .NET allows Directories and File Management in two ways.

1. Directory and File Class

2. Using FileStream Class